开发者

Disabling PostBack for blank/default ListItem in DataBound DropDownList

开发者 https://www.devze.com 2023-03-16 22:57 出处:网络
I have a DropDownList that is populated from a datasource. After it is bound, I place an empty field at the top of the list so that it appears blank to the user (creating a sort of \'default item\').

I have a DropDownList that is populated from a datasource. After it is bound, I place an empty field at the top of the list so that it appears blank to the user (creating a sort of 'default item'). I have some code behind handling the SelectedInde开发者_如何学编程xChanged event, but it doesn't really need to be executed if the user were to select the empty ListItem.

Here is my code:

.aspx

<asp:DropDownList ID="dropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropDownList_SelectedIndexChanged">
</asp:DropDownList>  

C# Codebehind adding the blank ListItem

dropDownList.Items.Insert(0, new ListItem(String.Empty, String.Empty));
dropDownList.SelectedIndex = 0;

Since I don't need the page to do a postback when the user clicks just this specific index, I want to disable postback entirely for just this listitem. Is this even possible? If so, how?


Set disabled="disabled", this will make the item not selectable (and not do postback)

<asp:DropDownList runat="server" ID="dddl">
    <asp:ListItem Text="" disabled="disabled" Selected="True"></asp:ListItem>
    <asp:ListItem Text="test"></asp:ListItem>
</asp:DropDownList>

Alternatively if you want to be able to select the first (empty) item but not do postback, do this:

<asp:DropDownList runat="server" ID="dddl" AutoPostBack="true" onchange="if(this.selectedIndex == 0)return false;">
    <asp:ListItem Text="" Selected="True"></asp:ListItem>
    <asp:ListItem Text="test"></asp:ListItem>
</asp:DropDownList>


You could add a required field validator. Then set the CausesValidation property of the DropDownList to true. This will prevent the postback and also provide the end-user feedback.

    <asp:DropDownList ID="dropDownList" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="dropDownList_SelectedIndexChanged" CausesValidation="true">
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="rfvDropDownList" runat="server" ErrorMessage="Must select a value!" ControlToValidate="dropDownList" />


Could you add an onChange attribute to run a JS function that checks if the value is empty before posting back?

i.e.

dropDownList.Attributes.Add("onChange", "javascript: return ddlChange()");

function ddlChange
{
    if(document.getElementById("<%= dropDownList.ClientID %>").value == "")
        return false;

    return true;
}


1.web control use postback, add attribute to cancel postback. but option will not be selected.

protected void rblRecentOrder_DataBound(object sender, EventArgs e)
{
    RadioButtonList rbl = sender as RadioButtonList;
    ListItem itemX = rbl.Items.FindByText("NA");
    // if no NA item then add it
    if (itemX == null)
    {
       itemX = new ListItem("NA");
       // set item cancel postback, but option will not be selected
       itemX.Attributes.Add("onclick", "return false;");
    }
}

2.or use javacsript call postback when need

<script>
function myPostback() {
     // call asp.net postback
     __doPostBack('tagName', 'params');
}

// TextBox
function updateValue(id, value) {
    let obj = document.getElementById(id);
    obj.value = value;
}

// Label
function updateInnerHTML(id, value) {
    var obj = document.getElementById(id);
    obj.innerHTML = value;
}

// DrowdownList
function setSelectOption(id, value) {
    let obj = document.getElementById(id);
    if (obj)
        obj.value = value;
}

// RadioButtonList
function selectRadio(id) {
    let obj = document.getElementById(id);
    // input type=radio
    if (obj) {
        let items = obj.getElementsByTagName("INPUT");
        for (var i = 0; i < items.length; i++) {
            items[i].checked = true;
        }
    }
}
</script>

c#
switch(Request.Form["__EVENTTARGET"])
{
    case "tageName":
        var params = Request.Form["__EVENTARGUMENT"];
        //..........
        break;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号