2013-12-15 15 views
2

信息,我有一个ASP.NET DataList,与页脚定义这样的:如何从DataList的脚注得到buttonClick

<FooterTemplate> 
    <asp:DropDownList ID="ddlStatusList" runat="server"> 
    </asp:DropDownList> 
    <input id="txtNotes" type="text" placeholder="Notes" /> 
    <asp:Button runat="server" type="button" Text="Add" ID="btnAdd"></asp:Button> 
</FooterTemplate> 

我正在寻找做的,就是对的btnAdd点击,得到值从txtNotesddlStatusList,但我不能解决如何访问控件,更不用说值。

我不能按照像this这样的东西,因为我无法检查我的按钮是否已被点击(尽可能用复选框),即使如此,我不知道我是否会如展示的那样能够使用findControl。 (“有不同的小号页脚的行为,以一个项目?

我不能使用Button难道一个DataList)”,因为当数据绑定,输入的文本不会同时存在,commandName & commandValue属性,因此我不能设置CommandValue

我已经使用LinkButton而不是普通的.NET Button不过来翻过了同样的问题,因此我不能工作了如何从TextBox/DropDownList

回答

2

下面应该工作得到的值尝试。见我添加了txtNotes RUNAT = “服务器”:

ASPX:

<FooterTemplate> 
    <asp:DropDownList ID="ddlStatusList" runat="server"> 
    </asp:DropDownList> 
    <input id="txtNotes" runat="server" type="text" placeholder="Notes" /> 
    <asp:Button runat="server" type="button" Text="Add" ID="btnAdd"></asp:Button> 
</FooterTemplate> 

C#:

protected void btnAdd_Click(object sender, EventArgs e) 
    { 
     var txtNotes = (System.Web.UI.HtmlControls.HtmlInputText)(((Button)sender).Parent).FindControl("txtNotes"); 
     var ddlStatusList = (DropDownList)(((Button)sender).Parent).FindControl("ddlStatusList"); 
    } 
+0

+1 - 优秀!!!回发时DataList的DataListItems为null。我认为这是最好的方法之一。 – afzalulh

0

您可以使用Control.NamingContainer访问行其他控件:

<FooterTemplate> 
     <asp:DropDownList ID="ddlStatusList" runat="server"> 
     </asp:DropDownList> 
     <input id="txtNotes" type="text" placeholder="Notes" runat="server" /> 
     <asp:Button runat="server" type="button" Text="Add" ID="btnAdd" OnClick="btnAdd_Click"></asp:Button> 
    </FooterTemplate> 

    protected void btnAdd_Click(object sender, EventArgs e) 
    { 
     Button btnAdd = (Button)sender; 
     DropDownList ddlStatusList = (DropDownList)btnAdd.NamingContainer.FindControl("ddlStatusList"); 
     System.Web.UI.HtmlControls.HtmlInputText txtNotes = (System.Web.UI.HtmlControls.HtmlInputText)btnAdd.NamingContainer.FindControl("txtNotes"); 
     int index = ddlStatusList.SelectedIndex; 
     string text = txtNotes.Value; 
    } 
+0

不幸的是,命名容器(DataListItem)在回发时为null。这将适用于GridView,但不适用于DataList。 – afzalulh