简体   繁体   English

我们如何处理 gridview/datalist 中下拉列表的 onselectedindexchanged 事件?

[英]How can we handle the onselectedindexchanged event of a dropdownlist in a gridview/datalist?

I have a simple datalist with a dropdownlist and a textbox.我有一个带有下拉列表和文本框的简单数据列表。

When the dropdownlist selected index changes, I want to load a value into the textbox in that listbox item (ie, the textbox on that particular row).当下拉列表选定的索引更改时,我想将一个值加载到该列表框项中的文本框(即该特定行上的文本框)。

<ItemTemplate>
    <asp:DropDownList runat="server" 
        ID="ddlCategory" AutoPostBack="true"
        DataTextField="category"
        DataValueField="category_code" 
        OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" />                      
    <br />
    Code<asp:TextBox 
        runat="server" 
        ID="txtOutputCode" 
        Text='<%# Bind("output_code") %>' />
</ItemTemplate>

How do I do this?我该怎么做呢?

The challenge I'm facing is how to find the corresponding textbox to update.我面临的挑战是如何找到相应的文本框进行更新。

Eg for a button I'd pass a commandname, and command arguments.例如,对于一个按钮,我会传递一个命令名和命令参数。 And then I'd handle the event in the gridview or datalist to find the corresponding textbox and update the text.然后我会处理 gridview 或 datalist 中的事件以找到相应的文本框并更新文本。 What can we do in case of a selectedindex change of a dropdownlist?如果下拉列表的选定索引发生更改,我们该怎么办?

I guess this should work.我想这应该有效。 Try this...试试这个...

protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
   var ddlList= (DropDownList)sender;
   var row = (GridViewRow)ddlList.NamingContainer;
   //get the Id of the row
   var Id = Convert.ToInt32(((Label)row.FindControl("IdColumn")).Text);
}

What you should do is the following:你应该做的是:

private SomeObject o = new SomeObject();

private void o_SomeEvent(...) {
}

public TheConstructor() {
    this.o.SomeEvent += new SomeHandler(o_SomeEvent);
}

which means you have to create a new dropdownlist and attach its event to the dropdownlist you have in the gridview on itemdatabound这意味着您必须创建一个新的下拉列表并将其事件附加到您在 itemdatabound 的 gridview 中的下拉列表

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlcouse = (DropDownList)sender;
        DataListItem Grow = (DataListItem)ddlcouse.NamingContainer;
        foreach (DataListItem dst in dstAllSite.Items)
        {
            DropDownList ddl_AccountInfo = (DropDownList)dst.FindControl("ddlAccountInfo");
            if (ddlcouse.SelectedIndex > 0)
            {
                BindAccountDDL(ddl_AccountInfo, ddlcouse.SelectedValue.Trim());
                ddlcouse.Focus();
            }
        }
    }

    private void BindAccountDDL(DropDownList ddlAccountInfo, string ddlcouse)
    {
        csPL.ddlcouse = ddlcouse;
        DataTable dt = csBL.BindAccountInfoDDL(csPL);
        if (dt.Rows.Count > 0)
        {
            ddlAccountInfo.DataSource = dt;
            ddlAccountInfo.DataTextField = "BankName";
            ddlAccountInfo.DataValueField = "BankId";
            ddlAccountInfo.DataBind();
            ddlAccountInfo.Items.Insert(0, "Select");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 子Gridview中不存在Dropdownlist控件的OnSelectedIndexChanged事件 - Doesnot firing OnSelectedIndexChanged event for Dropdownlist control existed in Child Gridview 我们如何从javascript触发OnSelectedIndexChanged事件 - How we can fire OnSelectedIndexChanged event from javascript GridView OnSelectedIndexChanged事件未触发 - GridView OnSelectedIndexChanged event not firing DropDownList的OnSelectedIndexChanged事件未触发 - DropDownList's OnSelectedIndexChanged event is not firing ASP:Dropdownlist OnSelectedIndexChanged事件未触发 - ASP:Dropdownlist OnSelectedIndexChanged Event Not Firing 如何从CreateUserWizard-&gt; WizardStep调用DropDownList的onselectedindexchanged事件 - how to call DropDownList's onselectedindexchanged event from CreateUserWizard->WizardStep 如何在OnSelectedIndexChanged触发事件之前获取DropDownList上的上一项 - How to get the previous item on DropDownList before OnSelectedIndexChanged fires the event 如何以编程方式选择GridView行,以便“OnSelectedIndexChanged”事件触发 - How to programmatically select GridView row so that “OnSelectedIndexChanged” event fires asp.net DropDownList的OnSelectedIndexChanged在数据列表之外 - OnSelectedIndexChanged of asp.net DropDownList which is outside datalist 更改DropdownList的选择会触发CheckBoxList的OnSelectedIndexChanged事件 - Changing selection for a DropdownList triggers OnSelectedIndexChanged event of a CheckBoxList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM