asphaltninja
asked on
Need button to programmatically add controls to a page
So I have a button on my page which I'd like to add a few textboxes and a fileupload control when clicked. It only works the first time the user clicks the button. Here's the code. Note: "pnlContainer" refers to an asp panel that's already on the page in markup.
protected void addFileUploader(object sender, System.EventArgs e)
{
int cnt = Convert.ToInt32(Session["cnt"]);
String codeBox = "txtCode" + cnt;
String versionBox = "txtVersion" + cnt;
String keywordBox = "txtKeyword" + cnt;
String uploader = "fUp" + cnt;
TextBox txtCode = new TextBox();
txtCode.ID = codeBox;
txtCode.Width = Unit.Pixel(70);
pnlContainer.Controls.Add(txtCode);
pnlContainer.Controls.Add(new LiteralControl(" "));
TextBox txtVersion = new TextBox();
txtVersion.ID = versionBox;
txtVersion.Width = Unit.Pixel(70);
pnlContainer.Controls.Add(txtVersion);
pnlContainer.Controls.Add(new LiteralControl(" "));
TextBox txtKeyword = new TextBox();
txtKeyword.Width = Unit.Pixel(70);
txtKeyword.ID = keywordBox;
pnlContainer.Controls.Add(txtKeyword);
pnlContainer.Controls.Add(new LiteralControl("<br/>"));
FileUpload fup = new FileUpload();
fUp.ID = uploader;
pnlContainer.Controls.Add(fup);
pnlContainer.Controls.Add(new LiteralControl("<br/>"));
cnt++;
pnlContainer.DataBind();
}
could not get the logic here... you have only one pnlContainer on your page I guess and each time you are adding to this panel? are you clearing the controls each time? if you dont clear, you should get multiple controls on panel... also I dont see how this sub is called from button click... are you using some repeater? how many "add" button do you have on your page? do you have a link to share? also can you please post the aspx code and also button_click code...
I think you forgot to update the session variable.
line 27: cnt++;
should probably read something like:
Session["cnt"] = Convert.ToString(cnt++);
line 27: cnt++;
should probably read something like:
Session["cnt"] = Convert.ToString(cnt++);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hello netswap and thanks for your help. How would I also be able to have new texboxes appear when the button is clicked?
Hi asphaltninja,
You can add additional textbox in same way, file upload control is added in javascript.
Below is the code snippet you can use.
var newUploadBox = document.createElement("in put");
uploadArea.appendChild(new UploadBox) ;
newUploadBox.setAttribute( "id", "txt" + addFileUploadBox.lastAssig nedId);
newUploadBox.setAttribute( "name", "txt:" + addFileUploadBox.lastAssig nedId);
Thanks,
netswap.
You can add additional textbox in same way, file upload control is added in javascript.
Below is the code snippet you can use.
var newUploadBox = document.createElement("in
uploadArea.appendChild(new
newUploadBox.setAttribute(
newUploadBox.setAttribute(
Thanks,
netswap.
ASKER
HainKurt
"could not get the logic here... you have only one pnlContainer on your page I guess and each time you are adding to this panel? are you clearing the controls each time? if you dont clear, you should get multiple controls on panel... also I dont see how this sub is called from button click... are you using some repeater? how many "add" button do you have on your page? do you have a link to share? also can you please post the aspx code and also button_click code..."
The "addFileUploader" is handling the button click event. Yes I have only one asp:panel named "pnlContainer" on the page. I'm not clearing the controls each time Here's the markup:
"could not get the logic here... you have only one pnlContainer on your page I guess and each time you are adding to this panel? are you clearing the controls each time? if you dont clear, you should get multiple controls on panel... also I dont see how this sub is called from button click... are you using some repeater? how many "add" button do you have on your page? do you have a link to share? also can you please post the aspx code and also button_click code..."
The "addFileUploader" is handling the button click event. Yes I have only one asp:panel named "pnlContainer" on the page. I'm not clearing the controls each time Here's the markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Individual.aspx.cs" MasterPageFile="~/core.Master" Inherits="fileSystemLoader.Individual" %>
<asp:Content ContentPlaceHolderID="headContent" runat="server">Individual Upload Page</asp:Content>
<asp:Content ContentPlaceHolderID="mainContent" runat="server">
<div>
<span style="color:red;">Press the "Add" button to include more files.</span>
</div>
<asp:Panel ID="pnlContainer" runat="server">
<asp:TextBox ID="txtCode" runat="server" HoveredStyle-BackColor="Yellow" Width="70px"/> <asp:TextBox runat="server" HoveredStyle-BackColor="Yellow" ID="txtVersion" Width="70px"/>
<asp:TextBox ID="txtKeyword" runat="server" HoveredStyle-BackColor="Yellow" Width="70px"/><br />
<asp:FileUpload ID="fUp" runat="server" Font-Names="Verdana"/><br />
</asp:Panel>
<asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="addFileUploader"/>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Upload" Font-Names="Verdana"/>
</asp:Content>
did you do this
Session["cnt"] = Convert.ToString(cnt++);
(suggested by Ironhoofs) also
are you setting Session["cnt"] = "1" on page_load?
Session["cnt"] = Convert.ToString(cnt++);
(suggested by Ironhoofs) also
are you setting Session["cnt"] = "1" on page_load?
ASKER
yes, I added the Session["cnt"] = Convert.ToString(cnt++);
and I set the session variable in page_load as so:
if(!this.isPostBack){
Session["cnt"] = 1;
}
should it not be inside the postback loop?
and I set the session variable in page_load as so:
if(!this.isPostBack){
Session["cnt"] = 1;
}
should it not be inside the postback loop?
ASKER
Okay netswap, your method appears to be working. I can retrieve the values for the files, but how to i retrieve the values from the texboxes?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi netswap,
the values in the text inputs are additional values that will eventually go into a database with the file, like keyword tags.
the values in the text inputs are additional values that will eventually go into a database with the file, like keyword tags.
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.