Clicking Linkbutton in a gridview doesn't fire the codebehind - asp.net

I have searched for a solution on this site and others and I have not been able to find my exact case so here goes...
I have a panel that is used as a modal popup by using the modal popup extender. Inside of this panel is an update panel that contains a multiview control. The views for the multiview contain grids. Different grid for different views. The left most field on the grid is a template field that contains a link button. This should fire the code behind to redirect to another page. Instead, it appears to reopen the popup with no data displayed.
So...here are the events as I perform them.
I click a button on the webpage that opens my popup that contains my grid. The correct view displays containing the correct grid and my data is correct. I click on my linkbutton that should direct me to a new page and instead I get the same popup with no data in the grid.
I have this exact functionality in use on another webpage, the only difference is that I don't use multiviews. However, I at least know that the linkbutton in the grid inside the update panel fires the code behind.
I have tried an image buttons it reacts the same. The hyperlink field does work and can be used, but I want to see if I can get the linkbutton to work so that I have a little more flexibility.

You can add Click event on your LinkButton, you add this when you iterate on your Grid in RowCreated.
void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton button= (LinkButton)e.Row.FindControl("IdButton");
button.Click += YourDelegate; //Adjust here your called delegate
}
}
Nota : you must ensure that you don't rebind your Grid every time that you post datas
You can use
if( ! IsPostBack)
{
//Bind your grid
}
You presist with ViewState

lick on my linkbutton that should direct me to a new page and instead I get the same popup with no data in the grid.
By that I suppose you click a link inside your modal Popup multiview's current view to navigate to another view. You can skip this altogether & use ASP.NET AJAX toolkit's tab control instead. You can place each gridview inside a separate tab & remove your link buttons.

Related

Onselectedindexchanged is firing

In grid row command event I am calling a .ascx page as a popup.
In the popup I have one button, on clicking the button, I'm calling one more popup.
Under the second popup I have four dropdown controls, in the dropdown controls I'm filtering and filling the data one after another.
The problem here is, if I filter the third dropdown, the second dropdown field data is getting reset(Onselectedindexchanged is firing).
You might be reloading/binding the drop downs on post back. You may want to change it to something like this
if(!Page.IsPostback)
{
//load the dropdowns the first time.
}

Button click event handler before page load

I will describe my problem in simple way so it's not exactly what I'm trying to do but the idea is the same.Here is the problem:
I create dynamic buttons from code behind.I get some id from query string,create button with that id ,dynamic add event handler to click event,and add button to placeholder.I store the list of id-s in session and in page load method recreate these buttons and add to placeholder.One of the id-s is CurrentId and it's also stored in session.Buttons click handler do something like this
Button b=(Button)sender;
Session["CurrentId"]=Convert.ToInt32(b.ID);
In page load when I create buttons I want to set button text property different from others if id==Convert.ToInt32(Session["CurrentId"]) when list of id-s are gotten from session.But problem is that click event handler is called after page load,and when I create buttons in page load ,CurrentId in session hasn't been channged by click event handler.Can you suggest any solution to this situation?
It looks like you are attempting to update the buttons you have dynamically created after the click event has fired. Why not just change the button text within the click event as you have described?
i.e.
protected void button_Clicked(object sender, EventArgs e)
{
((Button)sender).Text = "Custom text for active button";
}
Also, you can always update the buttons on the PreRender event which occurs after the control click events but before the controls are served back down to the client.

Avoid postback on gridview event

1
I've got popup (using javascript) working in a kind of gridview. When you click a button on one grid, it displays a popup window containing another grid of information, based on the row clicked in the first grid.
This works well... I've enabled editing in the grid that is popped up. When you click edit though, the popup window disappears. If I click the display button in the first grid though to bring the popup window visible again it displays, and is now in edit mode.
Is there a way to make postbacks in the popup not close the popup?
Hidden variable is way to go.
<input id="popupState" type="hidden" value="1" runat="server"/>
Set it from javascript and make the popup visible or invisible based on hidden variable's value in
jQuery(document).ready(function($) {
// your code goes here.
});
if you are using jQuery or if you are using Microsoft Ajax controls i.e script manager etc.
function pageLoad(sender, e)
{
//your code goes here
}
both of the javascript methods fires once the page is loaded on client. Better if you use jQuery one.

ASP.NET User Controls - Problem adding multiple instances programmatically?

I have created a user control which basically consists of 3 text boxes. I want to allow the user to click a hyperlink which will add a new instance of the user control onto a PlaceHolder. This seems to be working as I populate one of the text box controls with a random number which changes whenever I click the hyperlink. However, it is overwriting the previous control.
Heres the code on MyPage.aspx
protected void MyHyperlink_Click(object sender, EventArgs e)
{
var uc = new MyUserControl();
uc = (MyUserControl)LoadControl("~/path/to/my/usercontrol.ascx");
placeHolderCtrl.Controls.Add(uc);
}
Basically what I need to know is how can I get the control adding different instances underneath eachother as it just seems to be 1 control being overwritten each time.
Thanks.
The problem is after the postback, your previously added controls are not added to the placeholder. Dynamically added control should be added on each postback. My recommendation is to store a counter in a variable and at your page load, add your web controls again depending to this counter.

How do I add a textbox dynamically in ASP.NET?

I've a following requirement for my asp.net page:
User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink
He clicks submit button on page A and gets redirected to page B.
When he clicks on page A link from this page, the textboxes that he added should be persisted.
Can someone help me with the code on this?
Appreciate your help!
In the ButtonClick Method write.
TextBox tb = new TextBox();
Parent.Controls.Add( tb );
The Parent is the control you want to add the textbox to, for instance a panel.
You can also look at this resource.
Hope it helps.
Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.
This way you can handle both control creation and state persistence at the same time.
dealing with dynamic user controls can be a pain in the ass.
as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.
hope this helps.
Dynamically creating Textboxes:
suppose you have page like this
when you enter '1' in textbox and click on ADD button,output will be like display one textbox
below..
i have designed like this,
i have one textbox and placeholder for displaying dynamic textboxes..
double click on Add button...in btnadd_click,you have to write the following code
protected void btnadd_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < Convert.ToInt32(txtno.Text); i++)
{
TextBox txtbox = new TextBox();
phtxt.Controls.Add(txtbox);
phtxt.Controls.Add(new LiteralControl("<br>"));
}
}
debug it... and the output is,
As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.
I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.

Resources