I am having trouble when trying to fire a button in a GridView with the parameter CommandName = "x", I try to reach my "If" in the GridView1_RowCommand event but I just cant for some reason, if you could help me I'd be gratefull .
This is my .aspx part
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
EnableModelValidation="True" ForeColor="#333333" GridLines="None"
Height="193px" Width="968px" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SOURCE1" onrowcommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button Text = "Seleccionar" runat="server" CommandName="X" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="IDEmpresa" HeaderText="IDEmpresa"
SortExpression="IDEmpresa" />
</Columns>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
And this is my C# code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "X")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label1.Text = "WOW It reached out";
}
}
I followed the instructions of the ASP.net page, and im fairly new to .net (I dont know if the UpdatePanel has something to do with it)
Doing a quick test, the code you have provided works, but I did have to wire up my own datasource.
What you are missing here is that Label1 is outside of your UpdatePanel and will not refresh based on your localized postback within the UpdatePanel.
A word of caution with GridViews and UpdatePanels/Buttons. Make sure you are not manually binding/rebinding during Page_Load, and if you have code to do so, you do so within an if(!IsPostBack) { } statement.
Related
Hello Respected sirs,
I am generating a shopping cart like ordering system, in which i add/bind the productname, productprice, and productquantity from DataTable to GridView.
I have Added an ImageButton to the gridview only for deleting the selected row.
I also know that we can not delete a row from a dynamically generated grid view. so i placed a code in the ImageButton Click event that deletes the row from DataTable (Which is STATIC during the whole process) and again binds the Data With GridView.
Please note that i hv already once bind the data with gridview in my "BTN_ADD TO CART_Clicked".
Here is my code snippet,
protected void gvorderlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
DataRow row = dt.Rows[index];
dt.Rows.Remove(row);
gvorderlist.DataSource = dt;
gvorderlist.DataBind();
}
}
and ASP code is,
<asp:GridView ID="gvorderlist" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5"
onpageindexchanging="gvorderlist_PageIndexChanging"
onrowcommand="gvorderlist_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Cancel Order" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImgbtnCancelOrder" runat="server" CausesValidation="false"
ImageUrl="~/images/cross.PNG" OnClientClick="Javascript: return confirm('Aap Chutiye hai');" CommandName="Delete"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am getting an Error which says : The GridView 'gvorderlist' fired event RowDeleting which wasn't handled.
Any help will be appreciated...
Thank You
The error explains everything. You need todefine the event method for OnRowDeleting in markup:
<asp:GridView ID="gvorderlist" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5"
onpageindexchanging="gvorderlist_PageIndexChanging"
onrowcommand="gvorderlist_RowCommand" OnRowDeleting="gvorderlist_RowDeleting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Cancel Order" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImgbtnCancelOrder" runat="server" CausesValidation="false"
ImageUrl="~/images/cross.PNG" OnClientClick="Javascript: return confirm('Aap Chutiye hai');" CommandName="Delete"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And add an empty method in the code:
protected void gvorderlist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// No need to implement code here
}
The attributes for the GridView are case-sensitive, so change onrowcommand to OnRowCommand and see if that fires when you click the delete button. If not, you'll need to define OnRowDeleting explicitly. (Also change the capitalization of onpageindexingchange)
Give Command Name=D instead of Delete. It searches for Row_Deleting event when Command Name =Delete.
So here I'm trying to get the Textbox filled with the selected data from the DataGridView, so when I clicked the button in the DataGridView, the selected result will be put into the Textbox, I've tried the solution from other resource but still no luck. Can somebody help? Here's the DataGridView code:
DataGridView:
<asp:Panel ID="PanelDGV" runat="server" ScrollBars="None" Height="250" Width="515">
<asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" PageSize="8" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:BoundField DataField="ProjectCode" HeaderText="Project Code" />
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" />
<asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
</asp:GridView>
</asp:Panel>
notice the button is:
<asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
oh, and the textbox's ID is "TbProjectCode", and both are in separate pages, say that 1.aspx contain the textbox and a button to open the datagridview and 2.aspx contain the datagridview and the button to select the Project Code.
thank you
Add OnRowCommand event on your gridview: OnRowCommand="DGV_OnRowCommand".
Then add this on your code behind:
protected void DGV_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "CmdSearch")
{
int index = (int)e.CommandArgument;
GridViewRow row = DGV.Rows[index];
// Get the text on first cell of the row which is the project code.
TbProjectCode.Text = row.Cells[0].Text;
}
}
I have a gridview with a linkbutton that is posting crosspage when a value in the ID column is clicked. I want the ID to be posted to the next page so that it cant be seen in the url. (no querystring) The gridview is contained within a contentplaceholder. I would like to know how to response.write the linkbuttons (lbID) text on details.aspx using findcontrol. Please help. (Im using VB, I should have mentioned that.)
<asp:Content ID="content" ContentPlaceHolderID="content" runat="server">
<asp:GridView ID="gvOpen" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="Black" BorderStyle="None" BorderWidth="1px"
CellPadding="4"
ForeColor="Black" Width="96%" DataKeyNames="id"
DataSourceID="Open" CssClass="Grid" AllowPaging="True">
<AlternatingRowStyle BackColor="#CCFFCC" />
<Columns>
<asp:ImageField DataImageUrlField="priority" HeaderText="Priority">
<ItemStyle Height="28px" HorizontalAlign="Center" VerticalAlign="Middle" Width="28px" />
</asp:ImageField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:LinkButton ID="lbID" runat="server" PostBackUrl="~/details.aspx"> <%# Eval("ID") %></asp:LinkButton>
</ItemTemplate>
I think this is what you want: PostBackUrl
void Page_Load(object sender, EventArgs e)
{
LinkButton lbID = (LinkButton) PreviousPage.FindControl("lbID");
string linkText = "";
if(lbID != null)
linkText = lbID.Text;
}
You might want to set linkButton text like this
<asp:LinkButton ID="lbID" runat="server" PostBackUrl="~/details.aspx" Text='<%# Eval("ID") %>'></asp:LinkButton>
I have a gridview in my web page. I a linkbutton column. The commandName of the linkbutton column is "lbtnedit", I want When i click the linkbutton another tab will show detail of Resume by loading ID, but when i click linkbutton it don't run into gvresume_OnRowCommand
Thanks in advance !
Here my Grid:
<asp:UpdatePanel ID="udpsubtabResumeList" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="settingrow">
<div class="gSDMS_Grid">
<asp:GridView ID="gridViewResume" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%"
AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" PageSize="10" PagerSettings-Position="Bottom" PagerStyle-HorizontalAlign="Right"
CssClass="css_grid" OnItemCommand="gvresume_OnRowCommand" EnableViewState="true"
>
<AlternatingRowStyle CssClass='AlternatingRowStyle' />
<EditRowStyle CssClass='EditRowStyle' />
<FooterStyle CssClass='FooterStyle' />
<HeaderStyle CssClass='HeaderStyle' />
<PagerStyle CssClass='PagerStyle' HorizontalAlign="Right" />
<RowStyle CssClass='RowStyle' />
<SelectedRowStyle CssClass='SelectedRowStyle' />
<Columns>
<asp:TemplateField HeaderText="Full Name" ItemStyle-CssClass="txt" SortExpression="Fullname">
<ItemTemplate><%#Eval("Fullname")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="View" ItemStyle-CssClass="edit-del accept" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><a class="edit" href='<%# "/FutureEmployee/PostResume.aspx?&id=" + Eval("ResumeID") %>' title="Detail"> </a>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ItemStyle-CssClass="edit-del accept">
<ItemTemplate><asp:LinkButton runat="server" ID="lbtnedit" Text="Edit" CommandName="edit_cmd" CommandArgument='<% #Eval("ResumeID") %>'></asp:LinkButton>
</ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Here is event
protected void gvresume_OnRowCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName=="edit_cmd")
{
_id = new Guid(e.CommandArgument.ToString());
Response.Redirect(SiteRoot + "/FutureEmployee/EmployeeTab.aspx#subTabViewResume");
}
}
Edit one:
when i use firebug here is content in link:
href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$aaaaaaa$bbbbbbbbbbb", ""false, "",";Clients.aspx", false, true)
I think it fine if : javascript:__doPostBack('ctl00$mainContent$gridViewResume','resumeID') but i don't know how to do this ?
Asp:GridView hasn't event OnItemCommand.
You should use OnRowCommand and change signature of handler:
protected void gvresume_OnRowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit_cmd")
{
}
}
I solved proplem by remove all validate in my page.
As my gridview is populating I want to add an extra column with some buttons in but I can't seem to figure out how, or what might be the best way. Can anyone get me started?
Use a Template Column
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_OnRowCommand">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="SendMail"
Text="SendMail" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "SendMail") return;
int id = Convert.ToInt32(e.CommandArgument);
// do something
}