Ever since I added the Delete button, the Select button no longer redirects to another page when clicked. I added an alert on the OnSelectedIndexChanged and that worked so I am not sure why it no longer redirects. Also, if I remove the delete button the redirect works but I need both buttons.
<asp:CommandField ButtonType="Button" ShowSelectButton="True"></asp:CommandField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDeleteRecord" runat="server" Text="Delete" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this item?');" />
</ItemTemplate>
</asp:TemplateField>
OnSelectedIndexChanged. If I removed false it didn't matter either.
protected void listIndexView_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("mypage.aspx", false);
}
Hum, I can't say I seen this behvaour.
But, just drop in two templated buttons and you off to the races so to speak. Because you NOT REALLY using the built in edit and features of those buttons, then little is to be gained by "half" using the built in buttons that way. So just template two buttons like this:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelectD" runat="server"
Text="Select" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDeleteRecord" runat="server"
Text="Delete" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this item?');" />
</ItemTemplate>
</asp:TemplateField>
And as a result, you are free to attach events to each button to do whatever you want.
Note in the code editor, you will see this during editing of the markup and you ARE given a chance to create + add a event stub for buttons inside of that gridview etc.
Now if the button was outside of the gridview/listview/repeater etc., then you can normally just double click on the button and jump to the codebehind IDE. But when the button is inside of a repeating object, then you can't double click to create + build the event stub, but if you edit in markup as per above, then note how you see that dropdown appear - and you can choose create new event. Choose that, but you THEN have to flip over to code behind - and you will/should see the code stub.
Related
I have a ListView nested inside the column of a GridView, which looks something like this:
<asp:panel id="myPanel" Runat="server" EnableViewState="False">
<asp:GridView id="myDescription" AutoGenerateColumns="False" Width="100%" runat="server"
OnRowCommand="my_RowCommand" OnRowDataBound="GetDataForListView" EnableViewState="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="data1" HeaderText="Thing 1">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="data2" HeaderText="Thing2">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="data3" HeaderText="Thing3">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:TemplateField SortExpression="id">
<ItemTemplate>
<asp:HyperLink id="hlView" runat="server" NavigateUrl="...">View</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column to contain my list of things">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<% /* This is my list view: */ %>
<asp:ListView ID="myListView" runat="server" OnItemCommand="DoSomething" EnableViewState="False">
<ItemTemplate>
<asp:LinkButton ID="lbAttachment" Runat="Server" Text='<%# Eval("FILE_NAME") %>'
CommandName='<%# Eval("ROW_NUM") %>' CausesValidation="False" >
</asp:LinkButton><br/>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:panel>
The data is successfully binding, but the OnCommand event is not being fired on clicking the LinkButton (nor is any other event for the LinkButton, such as an OnClick event).
The rendered HTML for the link button shows that on clicking it, the page is performing a postback: javascript:__doPostBack('...','')
This means it is going back into my 'Page_Load' and refreshing the contents of the page - the grid view is binded here.
I can stop it from performing a postback by adding this attribute to the LinkButton:
OnClientClick="return false;"
But this only stops the postback from occurring, the OnCommand event still doesn't fire.
Any ideas?
The event signature in the code-behind is:
protected void DoSomething(object sender, CommandEventArgs e) { ... }
I have also tried using an OnItemCommand event on the ListView control with this event signature, but similarly the event is not invoked:
protected void DoSomething(object sender, ListViewCommandEventArgs e)
The OnRowdataBound event on the parent GridView is successfully invoked, it's only the nested ListView that fails to invoke its event.
The code I have shown is for the 2nd GridView on the page, there is another one too, and it is this one which gets binded on the Page_Load event. The Page Load event has a sequence of events as follows, where the 1st GridView (which we'll call GridView1) is bound:
Page_Load
Data for GridView is retrieved from database
Data is assigned to a 'DataView' object and assigned to the GridView1 DataSource property.
GridView1.DataBind() is invoked
Miscellaneous conditional logic which removes certain columns from GridView1
An OnClick attribute is added to each row.
So actually, the Page_Load binds the first GridView. The 2nd GridView is the one which contains the ListView which I am having a problem with. This 2nd GridView (the code of which is at the top of the post) is populated on the 'OnRowCommand' event of the 1st GridView, and has a sequence like this:
GetDataForListView
Get data from database
Assign the DataSet containing the data to the DataSource property of the 2nd GridView
Call the DataBind() method
Then, as you can see from the code I posted at the top, I have the OnRowDataBound event which fails to invoke its event in the code-behind.
Ciaran, I replicated the whole scenario and found out the below issue.
Issue: When any event related to either the GridView2 or ListView or the LinkButton is being fired it first hits the Page_load function. In the Page_load function you are not data binding GridView2 and the ListView so apparently they almost vanish and so the Event will not be fired.
Possible Fix:
You need to bind the GridView2 in the Page_load function in order fire those related events and the same goes with the ListView Too.
This fix is a very big one. I am not sure if you could try this.
a. If you are binding the GridView2 depending on the condition generated by the GridView1 click then store those parameters in some hidden field.
b. Have a function named LoadGridView2(parameters) where you bind the GridView2 and the ListView depending on the parameters passed and call this function in the Page_Load function each every time a PostBack is done and pass the stored parameters in the hidden field to the LoadGridView2(parameters)
c. By doing above you can bind the GridView2 according the GirdView1 Click condition and make the GridView2 and ListView available in the PostBack too. So now the event could be fired.
Let me know in case of any queries.
LinkButton has OnCommand, ListVIew has OnItemCommand. So the name isn't correct. OnCommand only fires if the LinkButton has a CommandName set. And also, the repeater may swallow the button's command argument. Certain controls do that because of the way events bubble up to the UI.
<asp:LinkButton ID="linkButton" Runat="Server" OnCommand="DoSomething"
CommandName="DoSomething"
CommandArgument='<%# Eval("ROW_NUM") %>' Text='<%# Eval("FILE_NAME") %>'>
</asp:LinkButton>
Or Try adding:
<asp:ListView .. OnItemCommand=".." />
And then do:
void DoSomething(Object Sender, ListViewCommandEventArgs e) {
// Do something
}
Your button would still need to set a command name like:
<asp:LinkButton .. CommandName="DoSomething" />
I know there are a lot of related posts or articles about this, but then it seems that they're not helping my case. I've even compared with a working sample at this site, http://www.ezineasp.net/post/ASP-Net-LinkButton-Command-Event.aspx, I don't think there's much difference. I thought my code should be working but apparently it just won't. I'm so sorry if this looks like a duplicate, but it's my last resort to post here.
Here's my HTML:
<asp:ListView runat="server" ID="AppsList">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
Sorry - Nothing found.
</EmptyDataTemplate>
</asp:ListView>
Code:
protected void AppsList_ItemCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
txtTest.Text = e.CommandArgument.ToString();
}
}
What I'm trying to achieve here is to capture the ID of the item in the ListView into the textbox when I click on either the Image Button or Link Button. Both will perform the same thing. I already got the Image button to work. When I click the Image, the ID, e.g 1 will appear in the textbox. But when I want to do the same thing with the Link Button, nothing will happen. The event is not being triggered in any way.
I've seen some posts talking about repeaters or AJAX to do the same thing, but I was just wondering why can't this code work. I would appreciate any pointer.
For my own experience, I have wrongly set ViewStateMode="Disabled" in page directive.
Because of this when post back occur upon clicking LinkButton inside the List View, existing data are vanished. So, will not reach to LinkButton's OnCommand event and never fire.
Once ViewStateMode attribute is removed, everything working well.
I noticed that your eventArgs should ListViewCommandEventArgs instead of CommandEventArgs and you should also bind the itemCommand event to your aspx page as shown below.
ASPX:
<asp:ListView runat="server" ID="AppsList" OnItemCommand="AppsList_OnItemCommand">
Code Behind:
protected void AppsList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Select"))
{
}
}
UPDATE:
Also remove the OnCommand attribute from image button and Link button OnCommand="AppsList_ItemCommand" , and each CommandName should be different than other.
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
Well, in case someone is wondering why this is happening, this is what I've found out after spending a great deal of time ripping off my codes to see what's wrong with it. I doubt that someone will bump into this issue, but to heck with it, sharing is caring.
Apparently the search button I've placed in my master page is the reason behind this. I've assigned its ID as "submit", which is the thing that is stopping my linkbutton from working, which I have no idea why that is happening. The search function is merely a search engine allowing users to look for the keywords in the webpage. The linkbutton's OnCommmand event just won't trigger when the ID to the search button is assigned as "submit".
When I change it to "submit1", everything works as normal. I'm still a newbie in this asp.net thing, can anyone tell me why is this even affecting the linkbutton? Anyway, the OnCommand event is now working properly.
I had a similar issue with LinkButtons. I don't remember how I figured this out, but I must of got fustrated and started clicking the LinkButton a few times fairly quickly and I noticed that the Command event did fire once in awhile. Soon I figured it's if I clicked on the LinkButton before the page fully loads that the command would fire correctly. If the page fully loaded then it seems something was hijacking that command on the LinkButton.
Without success, from one of the other suggestions I searched for any button that might be using the name/id "submit". There wasn't one on my page.
With success, I tried using ASP.Net Buttons instead of LinkButtons. That seemed to fix my issue. I'm not entirely sure what caused this situation but Buttons seem to work in my case so I'll stick with that.
I have a detailview that has the Delete button; when user clicks the Delete button they get the delete confirmation dialog box. What kind of command do i need to use in the detailview before the data gets deleted? When user selects "Yes" i want to save the data in the detailview into some kind of variable first then delete it. I want to get some kind of high level guidance or some hint. thanks
Here is the code for the confirmation dialog box in my ASPX file.
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete');" />
</ItemTemplate>
Subscribe to the ItemDeleting event, or even the ItemCommand event.
In both you will still be able to access the data before it is deleted.
I'm having an issue where the button within a radgrid is not firing until second time.
I have a usercontrol with a radgrid on it and button within one of its column.
The usercontrol is placed on a page.
When clicking the button on radgrid nothing happens 1st time, however it works 2nd time.
This is some of the radgrid column data
<telerik:GridTemplateColumn DataField="Quantity" HeaderText="Quantity" UniqueName="QuantityCol">
<ItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Columns="4" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' width="40px" />
<asp:LinkButton id='btnUpdateRow' runat="server" CausesValidation="false" Text='<span>Update</span>' CommandName="ButtonUpdateRow" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</ItemTemplate>
</telerik:GridTemplateColumn>
The radgrid uses NeedDataSource to obtain its data.
Page Load does not do anything.
During debug within itemcreated/itemdatabound, the linkbutton clientid it shows
TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow
However, when rendered to the browser it shows as
<a id="TestBasket_RadGrid1_ctl01_ctl09_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl09$btnUpdateRow','')">
Clicking the button - itemcommand is NOT fired.
On returning from postback the brower shows
<a id="TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl04$btnUpdateRow','')">
Clicking the button - itemcommand is fired.
Anyone explain why clientid gets changed.
I have tried placing a placeholder and creating the control with an id in itemcreated - still same issue.
Thanks in advance for any help.
Looks like the issue happened because of page inheritance.
I made a simple aspx page not using any of our architecture and it seemed to work fine.
I then bought the code back in and it stopped working.
I’ve managed to fix it by changing the page inheritance of the pages that use Telerik controls.
I am new to ASP.net thing, and i have some question regarding postback.
I have a Senario like this:
1) I have a grid on web with a panel inside.
2) I "Insert" the panel with a Web User Control by calling this
Control ctlControl;
ctlControl = LoadControl("~/UserControls/ChequeCreation.ascx");
pnlTransaction.Controls.Add(ctlControl);
3)The Web User Control providing two button. One is "update" and one is "reset".
Problem is like here:
What i wanted to achieve is when press the "update" button, it will update something back to my DB? But seem after i press the button "Update" or "Reset". The web user control is gone or missing. For my guest is because of the postback issues? Is that correct?
I tried if(!postback) still its doesn't work.
How am i going to overcome this? I already scratching my head about a day?
Thanks you so much.
Regards
LiangCk:
PS:Sorry for my english level, and please dont hesitate to voice out my error or mistake.
well you can convert any of your data columns to template column and then drag and drop your web user control to it
this will result in something like the following code check where "uc1:webUserControle1" is located in the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDB">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
<uc1:webUserControle1 ID="WebUserControle1_1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
if you are using AJAX, try add updatepanel on your UCT design page
ASP.NET will not preserve a dynamically added user control between postbacks. This is why it is dissapearing. You will need to add the control each time the page is created. However you will need to add it when the control tree is being initialized and restore the original control ID if you want your events to fire. These links provide a full explanation https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx and http://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/
You have to Every Time Reload the user control on Page_Init or
Page_Load. Then you can get the Button Click Event and After tha User
Control will not lost.
private void LoadUserControl(){
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath)) {
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
}
}
protected void Page_Load(object sender, EventArgs e) {
LoadUserControl();
}