在Datalist里面的Accordion里面的TextBox ... findcontrol无法正常工作

时间:2012-11-13 19:14:48

标签: c# asp.net accordion datalist findcontrol

我搜索过,但没有运气......

我在手风琴控件中有一个文本框,它位于一个数据列表中......我想让手风琴表单提交一些值,但是我不能从文本框中获取这些值,并且findcontrol不能正常工作

<asp:DataList ID="AddProjectDataList" runat="server">
<ItemTemplate>

 <asp:HiddenField ID="clientid" runat="server" Value='<%# Eval("mmmclientlistid") %>'></asp:HiddenField>
  <asp:Table ID="ProjectTableClass" runat="server" style="width:600px;height:600px"><asp:TableRow><asp:TableCell VerticalAlign="Top">
<b>New <asp:Label ID="ProjectTypeLabel" Text='<%# Eval("ProjectTypeName") %>' runat="server"></asp:Label> Project</b>
      <table class="AddProject" cellpadding="5">
          <tr>
              <td valign="top">
                  <b>Campaign</b> information:
              </td>
              <td>


                  <asp:DropDownList ID="DDCampaignList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnChange_selectCampaign" AppendDataBoundItems="True" >
                      <asp:ListItem Text="SELECT A CAMPAIGN:" Value="-1"></asp:ListItem>
                  </asp:DropDownList>
                  <br />



                  <ajaxToolkit:Accordion
ID="CampaignAccordion"
runat="Server"
SelectedIndex="1"
HeaderCssClass="accordionHeader"
HeaderSelectedCssClass="accordionHeaderSelected"
ContentCssClass="accordionContent"
AutoSize="None"
FadeTransitions="true"
TransitionDuration="250"
FramesPerSecond="40"
RequireOpenedPane="false"
SuppressHeaderPostbacks="true">
<Panes>
    <ajaxToolkit:AccordionPane ID="AccordionPane1" runat="server"
        HeaderCssClass="accordionHeader"
        HeaderSelectedCssClass="accordionHeaderSelected"
        ContentCssClass="accordionContent">
        <Header> 
            <asp:LinkButton ID="AddCampaignLink" runat="server">Or Click to Add New Campaign</asp:LinkButton> </Header>
        <Content> 
                             <asp:TextBox ID="campaignNameTextBox" Style="width: 400px" runat="server"></asp:TextBox><br />
                  Description (optional):<br />
                  <asp:TextBox ID="campaignDescriptionTextBox" runat="server" TextMode="MultiLine"
                      Columns="30" Rows="3"></asp:TextBox>
            <br />
            <asp:Button ID="AddCampaignButton" runat="server" Text="Add New Campaign" OnClick="AddCampaign_Click" />
        </Content>
    </ajaxToolkit:AccordionPane>        
</Panes>            
<HeaderTemplate>...</HeaderTemplate>
<ContentTemplate>...</ContentTemplate>

然后在代码隐藏

protected void AddCampaign_Click(object sender, EventArgs e)
    {
        //click to add campaign
        //campaignname
        //clientid



            HiddenField EID = (HiddenField)FindControl("HiddenFieldEID");
            TextBox campaignNameTextBox = (TextBox)AddProjectDataList.Items[0].FindControl("campaignNameTextBox");
            TextBox campaignDescriptionTextBox = (TextBox)AddProjectDataList.Items[0].FindControl("campaignDescriptionTextBox");

            tbl_campaign newcampaign = new tbl_campaign();
            newcampaign.clientID = Convert.ToInt32(DDClientList.SelectedValue); 
            newcampaign.employeeID = Convert.ToInt32(HiddenFieldEID.Value);
            newcampaign.campaignName = campaignNameTextBox.Text;
            newcampaign.campaignDescription = campaignDescriptionTextBox.Text;


            db.AddTotbl_campaign(newcampaign);

            db.SaveChanges();
    }

如果我摆脱手风琴窗格,它可以正常工作。控件位置没问题。但是随着手风琴,没有这样的运气。 (“hiddenFieldEID”字段位置很好,因为它在手风琴之外。)

我是否需要在DataList控件中使用单独的onItemDataBound事件?如果是这样,那里有什么,以及如何将它与buttonclick事件联系起来?

谢谢!

1 个答案:

答案 0 :(得分:1)

好吧,我可能刚想通了。

我这样做了:

                Control CampaignAccordion = (Control)AddProjectDataList.Items[0].FindControl("CampaignAccordion");
            TextBox campaignNameTextBox = (TextBox)CampaignAccordion.FindControl("campaignNameTextBox");
            TextBox campaignDescriptionTextBox = (TextBox)CampaignAccordion.FindControl("campaignDescriptionTextBox");

它有效。没有单独的数据绑定事件。这是正常的做法吗?