Adding FileUpload control via code-behind - asp.net

Because of the environment I work in I need to add controls to a page via code-behind. I have done it dozens of times. For some reason the FileUpload control is giving me grief. here is my code:
FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUploadControl";
this.Controls.Add(fileUpload);
The page looks as though it is timing out and display this error, "Internet Explorer cannot display the webpage".
When I remove the last line (the Add), then the page renders just fine.
Any ideas?
Thanks!!

You didn't mentioned which event handler you have used. Please try this,
FileUpload file;
protected void Page_Load(object sender, EventArgs e)
{
file= new FileUpload();
PlaceHolder1.Controls.Add(file);
}
protected void Button1_Click(object sender, EventArgs e)
{
if(file.HasFile)
{
file.SaveAs(MapPath("~/" + file.FileName));
}
}

Related

Web Forms Response.RedirectToRoute and Ajax UpdatePanel?

I added a button in datalist in updatepanel (Web Forms/AJAX). Can we use Response.RedirectToRoute() in codebehind? I would like to send cat_id with it ofcourse if it's possible.
I can use Response.Redirect in button click :
protected void goto_Click(object sender, EventArgs e)
{
Response.Redirect("Page.aspx?cat_id=3", true); // working
}
I can't use Response.RedirectToRoute in button click:
protected void goto_Click(object sender, EventArgs e)
{
Response.RedirectToRoute("go", new { cat_id= 3 }); //not working
}
I was going to be crazy.. i saw it's visual studio's problem. Response.RedirectToRoute(); is working in UpdatePanel. However, i coudn't understand why it didn't work (because it's same code) for a length of time.
Cheers

How do i postback?

I have this web application which is supposed to move rows up and down on button click, but it does only when I close the webpage and re run my program from Visual Studio.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
DataView view = (DataView)SqlDataSource1.Select(
DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
}
}
Is this all I need ?
You can use the CommandName to trigger the functionality of your button clicks.
Example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx
You need to use a button click event. You logic is currently in the page load event, that's why its only firing once
You could also remove the if postback which would also work but is not the recommended solution
protected void Page_Load(object sender, EventArgs e)
{
DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
}

ASP.Net Open New Tab in Browser from CodeBehind

I need to open a browser tab from a link that is given to me by an asp.net code behind.
Normally I would have a link and target="_blank", but the link that I need is dynamic, so I must have the behavior of a _blank link from code behind.
Any Ideas?
If you have the data needed to create the link when generating the initial HTML, you can do something like this in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick="javascript:window.open('MyPage.aspx?Param=" + Param1.ToString() + "');"; }
}
If you're waiting for the PostBack to get the required data to build the link, you can send javascript down to the browser via the ScriptManager:
protected void Button1_Click(object sender, EventArgs e)
{
//process whatever you need to to get Param1
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('MyPage.aspx?Param=" + Param1.ToString() + "');",true);
}
You're looking for the Target property.

Click event not work properly

In my website I wrote that code:
protected void Page_Load(object sender, EventArgs e){ LinkButton lbtnTopicAddress = new LinkButton(); lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
}
But when I press on the link in run time, the caller doesn't go to the EventHandler method.
Why?
Note,
I wrote code like that in many pages in the same website but it work only in one page.
i added that code to many page in website but it worded only in one page every page has its specific code and no relation between them I hope you understand me thanks
I need help pleaseeeeeeee..........................
Did you mean to miss off a ;and a } here?
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
I assume you've put a breakpoint in to ensure it isn't being fired?
I'm not exactly sure but I've got a feeling that instead of Page_Load you need to use Page_Init so your code would look this this:
protected void Page_Init(object sender, EventArgs e)
{
LinkButton lbtnTopicAddress = new LinkButton();
lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;
}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e)
{
Server.Transfer("~/SpecificTopic.aspx");
}
p.s. 5 mins formatting your code can work wonders when trying to debug
Are you adding the button to the controls on your page, or are you trying to find the "lbtnTopicAddress" control on your page?
Simply declaring the button won't do anything -- you have to get a reference to the control itself from the page.

how to display the mark sheet on the next page using ASP.NET?

i want to creat a web page that uses radiobuttons in a multiple choice quetions, but i want to do is to use a submit button to return the results to the next page with the mark sheet, so can you please help me with that. how to display the mark sheet on the next page using ASP.NET
Use Session state.
For example,
protected void Button1_Click(object sender, EventArgs e)
{
Session["key"] = TextBox1.Text;
Response.Redirect("marksheet.aspx");
}
Code in Page_Load of marksheet.aspx,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["key"] != null)
{
object val = Session["key"];
...
}
}

Resources