I'm binding an array to GridView. Below is my template field and it doesn't fire RowUpdating when I click on update.
<asp:TemplateField HeaderText="Role">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Container.DataItem.ToString() %>' ID="txtEditRole"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
This happened after making the field to TempleteField. Earlier the field was like below.
<asp:BoundField DataField="!" HeaderText="Role" />
Make sure you specify OnRowUpdating="gv_RowUpdating" event and also change FieldName in <%#Eval("Role") %>, see this example:
.aspx page
<asp:GridView ID="gv" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" OnRowEditing="gv_RowEditing"
OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowCommand="gv_RowCommand" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lbtnUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="lbtnCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role">
<EditItemTemplate>
<asp:TextBox ID="txtEditRole" runat="server" Text='<%#Eval("Role") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%#Eval("Role") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//your code here..
}
To check complete article, checkout insert, update, delete gridview data example in asp.net.
Related
I have gridview,which has approve and reject button.When click on reject button i need to show textbox for comments else no.after enetering comments i have save it in data base.How to do this can any one help me
here is my code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Industrial Zone">
<ItemTemplate>
<asp:Label runat="server" ID="lblindzone" Text='<%# Eval("indzone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="District">
<ItemTemplate>
<asp:Label runat="server" ID="lbldstr" Text='<%# Eval("dstr")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Industrial Area">
<ItemTemplate>
<asp:Label runat="server" ID="lblnmindar" Text='<%# Eval("nmindar")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plot Number">
<ItemTemplate>
<asp:Label runat="server" ID="lblplno" Text='<%# Eval("plno")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:LinkButton ID="lnkApproved" runat="server" Text="Approve" OnClick="lnkApproved_Click"><img src="images/approve.png" style="width:20px;height:20px; margin:5px" title="Approve"/></asp:LinkButton>
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
<asp:LinkButton ID="lnkviewdetails" runat="server" Text="View Details" OnClick="lnkviewdetails_Click"><img src="images/viewdetails.png" style="width:20px;height:20px; margin:5px" title="Details" /></asp:LinkButton>
<asp:TextBox ID="txtcomment" runat="server" style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="lblstatus" Text='<%# Eval("status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am assuming you are doing it with only C# and no Javascript. to view or hide a specific textbox you should trigger a RowCommand Event of GridView:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "viewhide")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tbComments = ((TextBox)gvr.FindControl("txtcomment"));
tbComments.Visible = true;
}
}
And this would be your ASPX Codebehind
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" CommandName="viewhide" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
EDIT: Now You've changed the tag from C# to VB.NET which is not good. because i consumed all my time writing a c# code!
I have an existing SQL database and a ASP.NET application. My application has two existing GridViews and login functions. I also have an existing Crystal Report designed to automatically create a receipt from my SQL database. This is done by the user filling out 3 specific parameters, and the rest of the data (which are in parallel with those parameters) will automatically fill out the crystal report.
I want to create a print button in my GridView to automatically fill out the 3 parameters in the Crystal Report. This is an attempt to make my application more user friendly. In short, the user would push the print button in its new column in the GridView, and the 3 parameters would be automatically picked up and filled into the Crystal Report.
My parameters are: "EmpID", "KeyControl", and "ControlNumber". My crystal report label is "x.rpt"
Here is my GridView markup:
<asp:GridView ID="gridKeyAndBuildingInformation" runat="server" CssClass="style3"
AllowSorting ="True"
AutoGenerateColumns ="False"
AllowPaging="True"
DataKeyNames="KeyRefId"
OnRowCancelingEdit="gridKeyAndBuildingInformation_RowCancelingEdit"
onPageIndexChanging="gridKeyAndBuildingInformation_PageIndexChanging"
OnRowDataBound="gridKeyAndBuildingInformation_RowDataBound"
OnRowEditing="gridKeyAndBuildingInformation_RowEditing"
OnRowUpdating="gridKeyAndBuildingInformation_RowUpdating"
OnRowCommand="gridKeyAndBuildingInformation_RowCommand"
ShowFooter="True"
OnRowDeleting="gridKeyAndBuildingInformation_RowDeleting"
AlternatingRowStyle-BackColor="#EFEFEF"
EditRowStyle-VerticalAlign="Top"
HeaderStyle-BackColor="#77b218"
OnSorting="gridKeyAndBuildingInformation_Sorting"
BackColor="#CCCCCC"
BorderColor="#999999"
BorderStyle="Solid"
BorderWidth="3px"
CellPadding="4"
EnableModelValidation="True"
ForeColor="Black"
CellSpacing="2">
<Columns>
<asp:TemplateField HeaderText ="EmpID" HeaderStyle-CssClass="HeaderText" sortexpression="EmpID">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpID" runat="server" Text='<%# Eval("EmpID") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewEmpID" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="ControlNumber" HeaderStyle-CssClass="HeaderText" sortexpression="ControlNumber">
<ItemTemplate>
<asp:Label ID="lblControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewControlNumber" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="KeyNumber" HeaderStyle-CssClass="HeaderText" sortexpression="KeyNumber">
<ItemTemplate>
<asp:Label ID="lblKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewKeyNumber" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:ButtonField HeaderText="Print" ShowHeader="True" Text="Print" />
</Columns>
<EditRowStyle VerticalAlign="Top"></EditRowStyle>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
You could handle the RowCommand event, which would fire when you click that delete button in your ButtonField column. Add this to the end of your GridView declaration markup (after "CellSpacing="2"" but before the ">"):
OnRowCommand="gridKeyAndBuildingInformation_RowCommand"
And then, in your code behind you would need something like this (this is C#, if you need VB.NET let me know - this question isn't tagged with a server-side language):
protected void gridKeyAndBuildingInformation_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Get your ID for the row you're on
int ID = Convert.ToInt32(e.CommandArgument);
// Get the row the button was clicked in
GridViewRow row = gridKeyAndBuildingInformation.Rows[ID];
// Get the values you need from that row
int EmpID = row.Cells[0];
int ControlNumber = row.Cells[1];
int KeyNumber = row.Cells[2];
// Use those numbers to make your call to the Crystal Report
// I don't know what this part would look like.
}
You can read more about the RowCommand event on MSDN: GridView.RowCommand Event
I am trying to build a simple GridView.
I have a Products table and it contains ProductID and ModelName.
I am using LINQ to SQL
My GridView code is
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my datasource code is
<asp:ObjectDataSource ID="objData" runat="server" SelectMethod="GetAll" UpdateMethod="UpdateProduct"
DataObjectTypeName="TestLINQProto.tblProduct" TypeName="TestLINQProto.ProductsList"></asp:ObjectDataSource>
I have my update method written like this
[DataObjectMethod(DataObjectMethodType.Update,true)]
public void UpdateProduct(tblProduct product)
{
EshopDataAccess.UpdateProduct(product);//This will call a LINQ function to update the product
}
The issue is that the tblProduct that the UpdateProduct contain original values. It is not getting the updated values.
I think the issue is that you are using Eval method which only supports reading/fetching of data. Use Bind Method in your grid aspx code. Like this
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Bind("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Data gridview bind the recored with data set.Gird view consist the column "Compnayname"
whenever user click on the column of gridview "Company name" then menu or dropdown control will be display.
so how to add dropdown or menu control in gridview.
I want to data display in grid view control and user click on company name then Menu will be display which contain the information like Send msg, save company detail.
You have to make Template Column
<asp:TemplateField HeaderText="Compnayname">
<ItemTemplate>
<asp:DropDownList ID="ddlCompany" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
here isthe complete GRIDView
<asp:GridView ID="grdList" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="grdList_RowCancelingEdit" OnRowEditing="grdList_RowEditing" OnRowUpdating="grdList_RowUpdating" OnPageIndexChanging="grdList_PageIndexChanging" AllowPaging="true">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label SkinID="OneColLabel" ID="lblName" runat="server" HeaderText="Edit" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnbEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnbUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="lnbCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="drpEditName" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to bind Dropdownlist on code behind
I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).