Click event not work properly - asp.net

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.

Related

Can I call Gridview selected index function on pageload?

How can I call a gridview_SelectedIndexChanged event on page load?
Please guide me I'm using asp.net c#
Is this possible to use trigger method on page load?
You can:
protected void Page_Load(object sender, EventArgs e)
{
gridview_SelectedIndexChanged(gridview, null);
}

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();
}

Adding FileUpload control via code-behind

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));
}
}

RegisterOnSubmitStatement after client-side validation

I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.

problem with gridview method calling

Though i am setting in grid view
OnRowDeleting="GridView1_RowDeleting"
and cicking on the gridview i am getting the
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
as i am clicking on Delete link it should open the Delete Event
protected void GridView1_RowDeleting(object sender,GridViewDeletedEventArgs e)
but its not opening,
please point me where is my mistake.
The problem is about your the event arguments, you should use GridViewDeleteEventArgs for OnRowDeleting. GridViewDeletedEventArgs is used for OnRowDeleted event.
protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
OnRowDeleting Reference
OnRowDeleted Reference
Hope this helps !

Resources