【问题标题】:How can we handle the onselectedindexchanged event of a dropdownlist in a gridview/datalist?我们如何处理 gridview/datalist 中下拉列表的 onselectedindexchanged 事件?
【发布时间】:2012-02-23 07:48:55
【问题描述】:

我有一个带有下拉列表和文本框的简单数据列表。

当下拉列表选择的索引发生变化时,我想将一个值加载到该列表框项的文本框中(即该特定行上的文本框)。

<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>

我该怎么做?

我面临的挑战是如何找到相应的文本框进行更新。

例如对于一个按钮,我会传递一个命令名和命令参数。然后我会处理 gridview 或 datalist 中的事件以找到相应的文本框并更新文本。下拉列表中的 selectedindex 发生变化,我们该怎么办?

【问题讨论】:

    标签: c# asp.net drop-down-menu datalist selectedindexchanged


    【解决方案1】:

    我想这应该可行。试试这个...

    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);
    }
    

    【讨论】:

    • 对于数据列表,使用 DatalListItem 代替 GridViewRow。
    【解决方案2】:

    你应该做的是:

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

    这意味着您必须创建一个新的下拉列表并将其事件附加到您在 itemdatabound 的 gridview 中拥有的下拉列表中

    【讨论】:

      【解决方案3】:
      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");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多