Вопрос

I'm unable to retrieve the content of a textbox that's inside an accordion panel. My markup is as follows:

<juice:Accordion ID="Accordion1" runat="server">

<juice:AccordionPanel ID="AccordionPanel1" runat="server" Title="Media ID">
<PanelContent>
  <asp:Label ID="LabelMediaID" runat="server" Text="Media ID"     AssociatedControlID="TextBoxMediaID"></asp:Label>    
  <asp:TextBox ID="TextBoxMediaID" runat="server"></asp:TextBox>
</PanelContent>
</juice:AccordionPanel>

My server side code is triggered when the user clicks a button:

protected void ButtonSearch_Click(object sender, EventArgs e)
{
    // Retrieve controls within accordion panels
    TextBox TextBoxMediaID = (TextBox)AccordionPanel1.FindControl("TextBoxMediaID");
    string mediaID= "abc";
    if (TextBoxMediaID != null)
        mediaID= TextBoxMediaID.Text;

I'm able to successfully retrieve my textbox control but when I try to access its Text property it's always empty.

Can someone help me? I'm afraid I'm reasonably new to the world of ASP.NET, Juice etc. Thanks.

Это было полезно?

Решение 2

Option 1 - Quick fix

For your page, make ClientIDMode="static" in page directive and then use Request.Form["TextBoxMediaID"] to get value of your text box.

Option 2 - Debug

Go to debug mode and try AccordionPanel1.Controls[0].("TextBoxMediaID"); or AccordionPanel1.Controls[0].Controls[0].("TextBoxMediaID"); because you don't know how many server side controls are there. Simple solution would be to use find control recursive function.

Другие советы

It turned out that my problem was caused by having an ASP.NET project that used master pages.

If you're not using master pages then

TextBox TextBox1 = (TextBox)AccordionPanel1.FindControl("TextBox1");
string content= Request.Form[TextBox1.UniqueId];

works perfectly. However, when using a master page with a ContentPlaceHolder then use the following code:

ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("MainContent");
TextBox TextBox1 = (TextBox)AccordionPanel1.FindControl("TextBox1");
string content= Request.Form[cph.UniqueID + "$" + TextBox1.UniqueId];

Hopefully this will save someone a lot of time!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top