Seems to be a weird problem but my Linkbutton which is in GridView is not firing its event on 2nd Time.
In Detail:
I have a GridView which has linkButton in it which is firing an event. This event is fired perfectly on first time but not working(not posting back) when i click it on 2nd time.
<asp:GridView ID="dg1" runat="server" OnSorting="dg1_Sorting" OnRowCreated="GridViewSortImages"
SkinID="grid" Width="100%" Font-Underline="false" HeaderStyle-Font-Underline="false"
OnRowCommand="dg1_RowCommand" AllowPaging="True" HeaderStyle-HorizontalAlign="Left"
OnRowDataBound="dg1_RowDataBound" ShowFooter="true">
<Columns>
<asp:TemplateField ItemStyle-Width="15px">
<ItemTemplate>
<asp:ImageButton ID="imgbtndel" runat="server" OnClick="imgbtndel_Click" ImageUrl="~/css/Images/delete.gif"
OnClientClick="return confirm('Do you want to Delete')"></asp:ImageButton>
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Type ID" SortExpression="ID" ItemStyle-Width="60px"
HeaderStyle-Font-Underline="false">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnno" runat="server" ForeColor="#123B61" Text='<%#Eval("ID") %>'
OnClick="lnkbtnno_Click"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle Font-Underline="False" />
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description" ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%#Eval("Description") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="200px" />
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void lnkbtnno_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
txtaccid.Text = lnkbtn.Text;
Label lblDesc = lnkbtn.FindControl("lblDesc") as Label;
txtdesc.Text = lblDesc.Text;
}
your linkbutton not firing events for second time.Because some other events got fired in second time instead of your linkbutton. So in events stack linkbutton events got disabled. Checkit out whether you added dynamic control are firing or not. Make use developer tools like firebug(F12)
Related
After clicking the edit linkbutton of my gridview, I show the data on different text boxes which are not inside the gridview. I have a "Reset" button which i want to use to get back to the original values. But I am having problem to access those gridview data inside the button click handler and reset it.I tried using DirectCast() but its showing System.NullReferenceException.
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false" CommandName="editRecord" Text="EDIT" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
<ItemTemplate>
<asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HANGER">
<ItemTemplate>
<asp:Label ID="lblHANGER" runat="server" Text='<%# Bind("HANGER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
The backend vb.net code is-
Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
Dim vID As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
Dim vHanger As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
txtID.Text.Text = vID.Text()
ddlHanger.SelectedValue = vHanger.Text 'dropdown list that's why selectedValue used
End Sub
I have copied the portion of the code cause the gridview has lot more rows. I would appreciate if anyone please show me a solution.Thanks in advance.
First remove the following:
<ItemTemplate>
<asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false"
CommandName="editRecord" Text="EDIT"
CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>
And add:
<asp:CommandField ButtonType="Button" ShowSelectButton="True" SelectText="EDIT" />
That markup will allow your GridView to have an EDIT button which will switch the current selected row index correctly. System.NullReferenceException error you are recieving could be because the GridView3.SelectedRow is NULL/EMPTY, which also means GridView3 currently has NO selected index.
To make sure that GrieView3 indeed selected a ROW, you can add: the following right AFTER the
<SelectedRowStyle BackColor="Black" BorderColor="White" BorderStyle="Dotted"
BorderWidth="3px" ForeColor="White" />
right after
</Columns>
So your final GridView3 markup should look like:
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True"
SelectText="EDIT" />
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
<ItemTemplate>
<asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HANGER">
<ItemTemplate>
<asp:Label ID="lblHANGER" runat="server" Text='<%# Bind("HANGER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="Black" BorderColor="White" BorderStyle="Dotted"
BorderWidth="3px" ForeColor="White" />
</asp:GridView>
Then you can also use TryCast too, like so:
Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
Dim vID As String = TryCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label).Text
Dim vHanger As String = TryCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label).Text
txtID.Text.Text = vID
ddlHanger.SelectedValue = vHanger
End Sub
i'm new to asp.net. i'm doing one project. in that i've used one gridview and fetch datas from database using sqldatasource.
gridview code is
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="Library" CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowDataBound="GridView1_RowDataBound" >
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="Sl_No" HeaderText="Sl_No" SortExpression="Sl_No" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Author" HeaderText="Author"
SortExpression="Author" />
<asp:BoundField DataField="Publication" HeaderText="Publication"
SortExpression="Publication" />
<asp:BoundField DataField="Available" HeaderText="Status"
SortExpression="Available" />
<asp:TemplateField HeaderText="Availability">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Available") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RIUserTaken" HeaderText="RIUserTaken"
SortExpression="RIUserTaken" Visible="False" />
<asp:TemplateField HeaderText="Taken By" ShowHeader="False">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("RIUserTaken", "{0}") %>'></asp:Label>
<asp:Button ID="SendRequest" runat="server" Text="Button" Visible="False"
onclick="SendRequest_Click" CommandName="SendRequestCmd" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Taken Date" InsertVisible="False"
ShowHeader="False">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("TakenDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" /> </asp:GridView>
code is one of the column of the grid view. if i click on that button, i've to store that button contains rows all datas in string variable. like
string slno="";
slno=gridview1.cells[0].text; (i'm not sure it is correct or not)
plz anyone help me??
thanks in advance :)
From msdn, you should check this.
GridView.SelectedIndexChanged event
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{
e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";
}
}
Do these things
Step 1. Wire up a RowCommand Evnt to your GridView
Step 2. Inside that event at code-behind, do this
if(e.CommandName.Equals("SendRequestCmd"))
{
var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow;
// now access the cells like this
var clickedSLNo = clickedRow.cells[0].Text;
}
Update:
e.CommandSource definition
A instance of the System.Object class that represents the source of the command.
IButtonControl.CommandArgument definition
A control that implements the IButtonControl interface must implement the CommandArgument property and the CommandName property to indicate the argument and command name that are propagated to the Command event.
I have a gridview that will display customer information. I want it to update the results as the user types first and/or last name into the textboxes. Basically, to databind the gridview to it's datasource with the updated characters from the textboxes as the user types.
CODE EDITED:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Process.aspx.cs" Inherits="Reservations.WebForm3" %>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>Processing...</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="firstname" runat ="server" class="inputLarge" ontextchanged="firstname_TextChanged" />
<asp:TextBox ID="lastname" runat="server" class="inputLarge" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="457px">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="CustID" HeaderText="Cust ID">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="FirstName" HeaderText="First Name">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="City" HeaderText="City">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts></Scripts>
</asp:ScriptManager>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="firstname" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
protected void firstname_TextChanged(object sender, EventArgs e)
{
InventoryAppSoapClient inst = new InventoryAppSoapClient();
DataSet ds = inst.getCustomer(firstname.Text, "none");
GridView1.DataSource = ds;
GridView1.DataBind();
}
Everything works except the TextChanged event only first the first time. If I go back and change the text and tab off, nothing happens.
Have you tried adding an event handler for the OnTextChanged event of your text box.
Inside you could force the databind like so
Private void firstName_OnTextChanged(object sender, EventArgs e)
{
//Your datasource and parameters are already defined on the front end so there is no
//setup required
existing.DataBind();
}
Same goes for your text box lastName
Private void lastName_OnTextChanged(object sender, EventArgs e)
{
//Your datasource and parameters are already defined on the front end so there is no
//setup required
existing.DataBind();
}
And don't forget to add the methods to the OnTextChange attribute for each textbox in the front end. Like so.
<asp:TextBox ID="firstname" OnTextChanged="firstName_OnTextChanged" runat ="server" class="inputLarge"/>
<asp:TextBox ID="lastname" OnTextChanged="lastName_OnTextChanged" runat="server" class="inputLarge" />
Also, if you embed this code in an update Panel and use the AJAX control toolkit (free) the grid will update seamlessly. Otherwise it won't be pretty if you use classic asp techniques. The page will keep posting back every time you press a key. It will get annoying.
I have a ModalPopupExtender which is, among other things, populated with a gridview (From a DataTable).
In this GridView I have a deletebutton attached to each row, which is supposed to delete the row. Is it anyway possible to delete the selected row from the datatable, and then update the GridView without closing the ModalPopupExtender?
Here is my GridView:
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupDragHandleControlID="divPopupReport" TargetControlID="btnHidden" PopupControlID="divPopupReport" CancelControlID="btnCloseReport" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>
<asp:UpdatePanel runat="server" ID="upReport">
<ContentTemplate>
<div id="divPopupReport" runat="server" style="text-align:left; padding-right:0px; background-color:White; border: 2px solid #87d000; display:none;" >
<asp:GridView ID="GridView2" runat="server" CssClass="list listExtended"
DataKeyNames="DocumentGuid" Width="100%" OnRowCommand="GridView2_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="DocumentName" HeaderText="Dokumentname">
<ItemStyle CssClass="list"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="CardName" HeaderText="Reference">
<ItemStyle CssClass="list"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="DocumentDate" HeaderText="Date">
<ItemStyle CssClass="list"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="">
<ItemStyle CssClass="list" />
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle CssClass="list" />
<ItemTemplate>
<asp:ImageButton ID="btnDelete" CssClass="image" runat="server" CommandName="Delete" CommandArgument='<%# Eval("DocumentGuid") %>' ImageUrl="~/delete.gif" Width="16" Height="16" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
And below my RowCommand.
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
DataTable SelectedDataTable = Session["SelectedDataTable"] as DataTable;
string guid = Convert.ToString(e.CommandArgument);
DataRow[] dr = SelectedDataTable.Select("DocumentGuid = '" + guid + "'");
SelectedDataTable.Rows.Remove(dr[0]);
Session["SelectedDataTable"] = SelectedDataTable;
GridView2.DataSource = SelectedDataTable;
GridView2.DataBind();
}
}
You need to wrap an UpdatePanel around the grid. This should solve the problem.
Another option is to use ajax.
Using Javascript/JQuery to remove the table row manually in combination with a WebMethod:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Another easy solution is to re-show the popup after post back.
Swap upReport panel with divPopupReport div (i.e. place upReport UpdatePanel into divPopupReport div).
I have a GridView that renders books from a database.
In each line a Delete/Edit button is rendered. When the user clicks the Edit button, I want the the Cancel and Update buttons to appear and the Edit buttton to become disabled.
I thought about using the onClick event for the Edit button along with the GridView row for getting the appropriate button based on the row, setting the Edit button's Enable property to false and the visibility of the Cancel and Update buttons to true.
However, it seems I can not change the properties even for the Edit button that I get from the event handler.
Here is the code.
protected void EditButton_Click(object sender, EventArgs e)
{
Button Sender = (Button)sender;
Sender.Text = "??"; //THIS CHANGE IS NOT APPLIED!!
//Button Sender = (Button)sender;
//GridViewRow grdRow = (GridViewRow)Sender.Parent.Parent;
//Button btn = (Button)grdBooks.Rows[grdRow.RowIndex].Cells[1].FindControl("CancelButton");
}
<asp:GridView
id="grdBooks"
DataSourceID="srcBooks"
DataKeyNames="Product_ID"
AutoGenerateColumns="false"
CssClass="products"
GridLines="none"
Runat="server" OnRowCreated="grdBooks_RowCreated">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button CausesValidation="false" ID="DeleteButton" CommandName="Delete" runat="server" Text="Delete" />
<asp:Button CausesValidation="false" ID="EditButton" CommandName="Edit" runat="server" Text="Edit" OnClick="EditButton_Click" />
<asp:Button CausesValidation="false" ID="CancelButton" Enabled="false" Visible="true" CommandName="Cancel" runat="server" Text="Cancel" />
<asp:Button CausesValidation="false" ID="UpdateButton" Enabled="false" Visible="true" CommandName="Update" runat="server" Text="Update" />
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:CommandField ButtonType="Button" ShowEditButton="true"/>--%>
<asp:BoundField
DataField="ISBN"
ReadOnly="true"
HeaderText="ISBN" />
<asp:BoundField
DataField="Title"
ReadOnly="true"
HeaderText="Title" />
<asp:BoundField
DataField="First_Name"
ReadOnly="true"
HeaderText="First Name" />
<asp:BoundField
DataField="Last_Name"
ReadOnly="true"
HeaderText="Last Name" />
<asp:BoundField
DataField="Price"
HeaderText="Price" />
<asp:BoundField
DataField="Quantity"
HeaderText="Quantity" />
</Columns>
</asp:GridView>
After the event is handled, the page probably reloads and resets the original text.
Maybe you could use JavaScript to do what you need.
Ok, it's very simple after all, there is this beautiful tag within the GridView that does exactly what i want..
Thanks for viewing, hope this post helps more niewbies..
<EditItemTemplate>
<asp:Button CausesValidation="false" ID="CancelButton" CommandName="Cancel" runat="server" Text="Cancel" />
<asp:Button CausesValidation="false" ID="UpdateButton" CommandName="Update" runat="server" Text="Update" />
</EditItemTemplate>