0

I have a dynamic control in a page. It was added to this page by codebehind.

FileUpload fu = new FileUpload();
fu.ID = "fu";
fu.EnableViewState = true;
list.Add(fu);

Button btnFu = new Button();
btnFu.Text = "Upload";
btnFu.ID = "btnFu";
list.Add(btnFu);

After using postBack, I can't get the value from this fileUpload.

if (!IsPostBack) {

}
else {
      string str_btn = null;
      if (Request.Form.Keys[Request.Form.Keys.Count - 1] != null)
                    str_btn = Request.Form.Keys[Request.Form.Keys.Count - 1].ToString();
      if (!string.IsNullOrEmpty(str_btn)) 
                    handleClick(str_btn);

}

Anybody has a solution to resolve this?

quadroid
  • 7,621
  • 5
  • 41
  • 71
Brian
  • 163
  • 2
  • 18

3 Answers3

0

I would try searching for the control using Page.FindControl("fu"). There are some interesting options in the answers to this question covering issues like looking for controls when using masterpages and user controls.

ASP.Net FindControl is not working - How come?

Community
  • 1
  • 1
leemicw
  • 612
  • 8
  • 13
0

I suspect the list you have added the controls to is a content container so you must first get a reference to it then call the FindControl("btnFu") method.

// var list = yourListControl
Button btnFu = list.FindControl("btnFu") as Button;
if(btnFu != null)
{
    // Operate on the button
}
DaveB
  • 9,308
  • 4
  • 36
  • 60
0

You need to recreate your dynamically created controls on a post back, I believe.

public class YourPage : System.Web.UI.Page
{
      protected bool AreControlsCreated {
      {
          get{return (bool)ViewState["AreControlsCreated"];}
          set{ViewState["AreControlsCreated"] = value;}
      }

      private void Page_Load(object sender, System.EventArgs e)
      {
          if(!IsPostBack)
          {
              AreControlsCreated = false;
          }
          else if(AreControlsCreated)
          {
              CreateYourControls();
          }
      }

      private void CreateYourControls()
      {
        ...
        FileUpload fu = new FileUpload();
        fu.ID = "fu";
        fu.EnableViewState = true;
        list.Add(fu);

        Button btnFu = new Button();
        btnFu.Text = "Upload";
        btnFu.ID = "btnFu";
        list.Add(btnFu);

        AreControlsCreated = true;
      }

      protected void YourButton_Click(object sender, EventArgs e)
      {
          createYourControls();
      }
}
mostruash
  • 4,079
  • 1
  • 21
  • 38
  • Hi brother,Thanks for your reply.I see your idea.I believe that it will work fine if we lost control.But in my case,It still show control,But I can't get value from this control.So that,My problem is that can't get value from FileUpload – Brian Mar 15 '13 at 07:32
  • How do you try to get the value? Show me your code. You are doing many things wrong including creating controls in behind code. You can put them in the markup as not-visible and make them visible upon a click of a button. Read some books/articles/blogs on best practices. – mostruash Mar 15 '13 at 10:11