LinkButton Text property not changing after dynamic change - c#

I have tried for so long to understand why my LinkButton text toggles to say "on" when its clicked, however when I click the button again the text still says "on" even though in my code behind file I set LinkButton.text = "off". I have run the debugger and when I set the text to "off" its like
the setter method doesnt actually update that specific LinkButton object, as the next click its like it never got updated.
I am using this to test why my panel inside a gridview template field wont toggle its visibility on and off, as the same thing happens with that where I click a button to make the panel visible which it does, then when I press the button again the panel should disappear but it stays on the screen.
My goal is to have each row in a gridrow have a toggle link button that displays a panel that drops down below each row.
So the essence to my problem is, why can I toggle a control on, but cannot toggle it off?.
My markup is essentially the same as below. Note all the actual proper server attributes are left
out for sake of clarity.
I've tried setting viewstate to false but that doesn't help.
The funny part is, I place the LinkButton and Panel outside of the
gridview and it does exactly what I expect it to do which is toggle
on and off or set the panel to visible or hide.
I ready some other posts but I don't fully understand the issue.
I have read that some people say its about nesting controls inside other controls but I am just not certain how this is causing an issue.
UpdatePanel.Visible = true has no effect
http://forums.asp.net/t/1773859.aspx?C+Visible+true+not+working
My other thought is that the button is rendered at a stage that is too late for me to make a change that will be placed on the final html?
Can anyone give me any pointers?
<asp:GridView>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div id="wrapper">
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'
Text="off" oncommand="changeName"></asp:LinkButton>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
protected void changeName(object sender, CommandEventArgs e)
{
GridViewRow row = MyGridView.Rows[Convert.ToInt32(e.CommandArgument)];
LinkButton button = (LinkButton)row.FindControl("LinkButton1");
if (button.Text.Equals("off"))
{
button.Text = "on";
}
else
{
button.Text = "off";
}
}

I solved the issue by removing my 'EnableViewState = "False"` on the gridview.
After reading this article
http://forums.asp.net/t/1590532.aspx?Visible+invisible+panel+inside+Gridview
It gave me some ideas on the whole postback process.
I am not sure if this is correct but it kind of makes some sense to me?
If my gridview is disabling viewstate and its the parent to all nested controls such as 'Panel' and 'LinkButton' then the state of those nested controls will be lost across postback. So any
dynamically made changes I modified in the code behind file would be lost if the changes were not
being retained in the viewstate of the gridview.
There is probably a better way of achieving my design but for the moment leaving viewstate
on the parent control seemed to fix it.

Related

Asp.Net, textbox textchanged problem in UpdatePanel

I am having a problem when the textchanged event of the textbox object is triggered. For example, when I want to press the tab key, it should focus on the next object, but it does not. Or when I click a button, I have to click twice. How can I solve this? I added the codes and the problem I was having as a gif.
default.aspx
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div class="form-group row">
<label class="col-lg-2 col-form-label">İşlem Tutarı</label>
<div class="col-lg-4">
<asp:TextBox ID="txtTutar" CssClass="form-control form-control-sm price" AutoPostBack="true" placeholder="0,00 ₺" runat="server" OnTextChanged="txtTutar_TextChanged"></asp:TextBox>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtTutar" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
default.asp.cs
txtTahsilEdilen.Text = txtTutar.Text;
You can dump the trigger - it will do nothing of value for you. You need to set AutoPostBack="true" for that text box - and the trigger will not help you.
Then what on earth is the trigger for then?
It is used to trigger ANOTHER SEPERATE panel on the same page!!!
So, you can have a button in a panel, even a text box. They STILL MUST do a post back here - and you need/have to add the post-back to the text box.
What the triggers do? Well, say you have a 2nd update panel - and has a bunch of things in it. Well, your code behind might update BOTH panels.
Hence, you would add a trigger to the 2nd panel that points to your text box (or button in the first panel). Now when that first panel update panel updates (due to a post-back), the 2nd panel with the trigger tag and settings will ALSO refresh.
However, the FEATURE DOES NOT AUTO POST back for you. So specifying a trigger into the SAME update panel is not required, does not make sense, and will do NOTHING at all for you.
Unless a post back IS TRIGGERED then the trigger settings do nothing at all, and they do not cause a post back, and they do not case the button code, or in this case the text box event code to run.
So, the triggers settings would in theory be placed in a 2nd and different update panel that you want to also re-fresh when one button (or text box) cases a post-back in the other update panel. However for this to work, YOU STILL MUST cause and have a post-back to occur.
So the purpose here is to not make the update panel post-back, but only to detect that a ANOTHER DIFFERENT panel REALLY DID JUST do a postback.
No matter which way we note and explain the above?
The trigger settings does NOT trigger the button or text box post-back - that is your job to make that occur. the trigger setting in the update panel can thus be set that some post back by a given button occurred, and then ensure that the panel with the trigger will also update. But for the same panel - there is no practical meaning or reason to do this, since the trigger tags only EVER work WHEN you cause a post-back. And the trigger settings are to ONLY point to buttons or controls in a DIFFERENT update panel.
Bottom line:
Remove the trigger tag - don't need it.
Set the text box to have auto postback = true

how to make text to get invisible from the page after a button click

I am developing a webpage in which I want to make text to get invisible after a button click and make gridview to be visible. In the page I have textboxes and button also, but those things can be made invisible by using "textbox.visible = false" and "button.visible = false" but I dont understand how to make the text to get invisible.
The design of the page is as shown below :
I want to make those "Search details", "Account number" and "Scriptname" to be invisible and only gridview to be visible after the button click ;
Please suggest how to do it.
You can include all the controls in an ASP.NET Panel control and hide\show them in code behind based on condition:-
<asp:Panel ID="SearchPanel" runat="server">
<%--put all the controls here--%>
</asp:Panel>
Then in code behind just use the panel control's id and change is visibility:-
SearchPanel.Visible = true;
SearchPanel.Visible = false;
By using panel, I was able to solve the problem.
HTML code of the page will be :
Account Number
Scriptname
Market value
In code behind page I change visibility of panel to false. (C# code is shown below)
Panel1.Visible = false;

Postback after clicking a button does not react not for the first time in a UserControl

I've a webapplication with UserControls. When I click on a menu-item or a select-button in a grid, than I see that didn't react the first time. When I clicked for the second time then an event behind the button will fire.
What do I wrong?
I've given the components a unique ID and the events are in the cs-files.
I hope someone can help me.
thanks.
The problems is in the two following examples:
<asp:Menu ID="TabMenu" Width="100%" Height="25px" runat="server"
Orientation="Horizontal" CssClass="TabPages"
StaticEnableDefaultPopOutImage="False"
AutoPostBack="true"
OnMenuItemClick="DoMenuItemClick" >
<Items>
<asp:MenuItem Text="Domains" Value="0"></asp:MenuItem>
In this case for the first time, when you clicked on a menu-item, it looks like the page only is reloaded. And when clicks for the second time, the event really fires. In this example when you clicks on a menu-item, the right View is shown.
<asp:GridView ID="gvwSelection" runat="server"
AllowPaging="True"
SelectedIndex="1"
AutoGenerateSelectButton="True"
OnSelectedIndexChanged="gvwSelectie_SelectedIndexChanged"
OnSelectedIndexChanging="gvwSelectie_SelectedIndexChanging" >
<PagerStyle ForeColor="#00257e" HorizontalAlign="Right"
BackColor="#FFFFFF"></PagerStyle>
</asp:GridView>
Example 2: The first time you click on a selection button, then the row you touched is not selected. And after the first time everything works correct.
The ID's have a fixed name.
The AutoPostBack is set to True.
There is an event linked.
The grids has DataBinded.
There is site.master a default.aspx and the UserControls are placed in a Placeholder.
I hope someone can help me.
I've found and solved the problem.
I have a dynamic UserControl FAddress and everytime he is wearing another control.
So I added the following row in the method: FAddress.ID = "UserControl1";
The problem was, that the control was loading everytime. By giving this dynamic control a fixed ID, this item has solved.
private void LoadPage(string APageName)
{
FAddress = null;
PlaceholderAddressTemplate.Controls.Clear();
if (!string.IsNullOrEmpty(APageName))
{
FAddress = (UserControl)LoadControl(string.Format("~/UserControls/{0}.ascx",
APageName));
if (FAddress != null)
{
FAddress.ID = "UserControl1";
PlaceholderAddressTemplate.Controls.Add(FAddress);
ShowOrHideComponents();
FAddress.Focus();
}
else
ShowOrHideComponents();
}
else
ShowOrHideComponents();
}
Ensure that the autopostback property is set to true behind your button, that may solve your problem.

Dynamically adding a command button to a GridView

I'm having an issue with trying to add a button to my grid. My GridView is first loaded with data in the PageLoad event.
I'm then taking the data in the first cell of each row, and creating a button that will link to a URL. To get the URL, I have to run a query with the data in the first cell as a parameter. I was doing this in the RowDataBound event at first, but hitting that query for every row was making it really slow.
So I decided to add a button that would retrieve the URL only when you clicked the button.
Here's my GridView:
<asp:GridView ID="gvResults" runat="server"
OnRowDataBound="gvResults_RowDataBound"
OnRowCommand="gvResults_RowCommand">
</asp:GridView>
And my code:
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
LinkButton lb = new LinkButton();
lb.CommandArgument = e.Row.Cells[0].Text;
lb.CommandName = "NumClick";
lb.Text = e.Row.Cells[0].Text;
e.Row.Cells[0].Controls.Add((Control)lb);
}
}
protected void gvResults_RowCommand(object sender, CommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "numclick":
string url = GetUrl(e.CommandArgument.ToString());
Response.Redirect(url);
break;
default:
break;
}
}
The grid generates fine, the button gets added to the grid for each row. But when I click on it, the RowCommand event doesn't fire, and the page just refreshes.
Does anyone know what the issue is?
Why use a dynamic button at all? You can easily put the linkbutton directly into the markup of the gridview (as long as you don't mind using a template field) and there will be no need to mess around with the RowDataBound event.
Your markup would look something like the following:
<Columns>
<asp:TemplateField HeaderText="SomeHeaderText">
<ItemTemplate>
<asp:LinkButton ID="lnkBtn" runat="server" CommandName="NumClick" CommandArgument= '<%# (string)Eval("dbValue") %>' Text='<%# (string)Eval("dbValue") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField></asp:BoundField>
<asp:BoundField></asp:BoundField>
<asp:BoundField></asp:BoundField>
</Columns>
Add breakpoints to the RowCommand event and make sure that you can hit the breakpoints.
The problem may lie elsewhere.
Also, make sure that you're not databinding on postback.
You have a big trouble with your code. It's pretty hard for me to explain what's your big mistake, but I can easily tell you how to fix.
The problem is that you generate a new button inside the RowDataBound event, definitely the wrongest choice. The button gets rendered because it exists after that event when page renders, but doesn't exist before data binding. If you bind data everytime you load the page (even during postback) the button still gets rendered because you generate a new button.
But since the button doesn't exist before data binding, it cannot raise events. You must declare the button from markup into a template of GridView, then access it not by using new LinkButton() but by using e.Row.Cells[0].FindControl("buttonId") and set its text. Then, you have to set its markup in order to fire its own Command event (not RowCommand) and handle it as you used (don't forget to set CommandArgument during data binding)
[Edit] I also made a mistake: controls inside data bound controls also don't exist before data binding. But they are initialized not with new Control() (by the private methods of data bound control) but with Page.LoadControl(typeof(Control)). That's the first thing you must fix when you load controls dynamically!!
Because the control is added dynamically on databind and you have to databind the gridview for each postback, the control being "clicked" is different each time. The event doesn't fire because at the time it needs to fire it doesn't exist as it did in the last iteration of the page.
I notice you don't have any logic determine if the button should be there, and it always goes into cell[0].
You should place this button into a TemplateItem so that it exists properly. If you have a need to do it in code-behind, you are probably better served doing it in the RowCreated event.

BulletedList onClick not firing

Ugh, this is driving me mad
Im trying to build up a dynamic menu from a bulletedList, most menu items are plain links however the log out button needs to perform some cleanup code.
I cant for the life of me get the BullettedLists onclick event to fire.
The BulletedList is inside a user control (if that makes a difference)
Any ideas?
Or - any ideas for an alternative, better solution?
Code below
BulletedList
<asp:BulletedList OnClick="menu_Click" runat="server" CssClass="MainMenu" ID="loggedInMenu" DisplayMode="HyperLink" />
Adding an element
loggedInMenu.Items.Add(new ListItem("Logout", ""));
Click handler
protected void menu_Click(object sender, BulletedListEventArgs e)
{
user.logout();
Response.Redirect("Default.aspx");
}
You're using the wrong DisplayMode for your BulletedList control. You should use a DisplayMode of LinkButton. When you use DisplayMode.HyperLink:
Users can click links to move to
another page. You must provide a
target URL as the Value property of
individual items.
This is from the MSDN docs for this control. (It's about 3/4 of the way down the page.)
When you use a BulletedList control in HyperLink mode, the value of your ListItem is the URL that you're navigating to. So your static page HTML controls would use ListItem.Value as the href attribute of the <a> tag.
Here's what the HTML markup looks like when you use a DisplayMode of HyperLink (it's a plain old HTML anchor tag w/ a href):
<li>One</li>
But since you want to postback, you should set the DisplayMode of your BulletedList control to LinkButton. When you do that, you'll enable a postback back to your page and your event handler will trap the event. You can then process the click appropriately then. The event argument that's passed in (of type BulletedListEventArgs) will have an Index property, and that will tell you what item in your list was clicked.
Here's the updated .aspx code that I used:
<asp:BulletedList ID="bullet" runat="server" DisplayMode="LinkButton"
onclick="bullet_Click">
<asp:ListItem Text="One" Value="1">One</asp:ListItem>
</asp:BulletedList>
Everything else is the same except the DisplayMode, which is set to LinkButton. When I use that, then my bullet_Click event handler is fired when I click a list item.
I hope this helps!!

Categories

Resources