Can I call Gridview selected index function on pageload? - asp.net

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

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

Page_Init and Page_Load

A page containts custom address control and checkBox. Why does the second example of code work properly, but first doesn't?
//1
protected void Page_Init(object sender, EventArgs e)
{
//doesn't work properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
//2
protected void Page_Load(object sender, EventArgs e)
{
//works properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
Because the viewstate of the controls is loaded between the init and the load event. This means that the init event does not know the state of the client yet.
MSDN lifecycle overview
Because all controls are create in OnInit() method, that call between Page_Init and Page_Load. In Page_Init all controls are null. Read more

databound dropdownlist select an item

I have a databound dropdown list (ASP.net). I want the page to load with a certain item as the selected item.
I am not adding a blank first row (thats not what i need)
I find that I can get this to work with "AppendDataBoundItems" to true, but the side-effect is that I have all the items listed twice.
thanks for your help!
Use the Page_PreRender event to handle this situation....
In the Page_Load register an event handler for the PreRender event
protected void Page_Load(object sender, EventArgs e)
{
Page.PreRender += new EventHandler(Page_PreRender);
}
And in the PreRender event,
void Page_PreRender(object sender, EventArgs e)
{
ComboBoxSomething.SelectedValue = WhatEverYouWant;
}

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.

Resources