GridView gv = new GridView();
BoundField farmername = new BoundField();
farmername.HeaderText = "Farmer Name";
farmername.DataField = "farmername";
gv.Columns.Add(farmername);
BoundField villagename = new BoundField();
villagename.HeaderText = "Village Name";
villagename.DataField = "village";
gv.Columns.Add(villagename);
BoundField feedtype = new BoundField();
feedtype.HeaderText = "Feed Type";
feedtype.DataField = "feedtype";
gv.Columns.Add(feedtype);
BoundField bf50kg = new BoundField();
bf50kg.HeaderText = "50 Kg Bags";
bf50kg.DataField = "noof50kgsbags";
gv.Columns.Add(bf50kg);
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Button;
cf.ShowCancelButton = true;
cf.ShowEditButton = true;
gv.Columns.Add(cf);
gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
gv.RowUpdating += new GridViewUpdateEventHandler(gv_RowUpdating);
gv.RowCancelingEdit += new GridViewCancelEditEventHandler(gv_RowCancelingEdit);
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtIndentDetails;
gv.DataBind();
当我点击编辑按钮时,它没有分成更新,取消按钮。如何使用命令字段执行此操作。如果我在aspx页面中添加gridview,则将其拆分为更新和取消
答案 0 :(得分:0)
用链接或图像按钮替换命令按钮。放一个衬垫。
cf.CommandName = "Edit";
您也可以对取消和更新执行相同操作,并分别传入命令名称“取消”和“更新”。这将是一个解决方法。我也无法触发动态生成的命令按钮。
<强>更新强>
另一种有用的干净方法。查看投递箱here
上的附件答案 1 :(得分:0)
分配命令名称,在事件处理程序中使用这样的代码进行编辑和更新。我用过vb.net。
Create: Link Button b= new Link Button()
cf.ShowEditButton = true;
cf.CommandName="Edit";
gv.Columns.Add(cf);
使用项目模板和编辑模板。当编辑命令激活时,将自动执行rowediting方法。
Protected Sub grdCustomers_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdCustomers.RowEditing
grdCustomers.EditIndex = e.NewEditIndex
GetAllCustomersData()
End Sub
更新:
Protected Sub grdCustomers_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim result As Boolean
Dim CustObj As New Services.Entities.Customers.Customer
CustObj.CustomerID = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("lblCustomerId"), Label).Text
CustObj.ContactName = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("txtContactName"), TextBox).Text
CustObj.City = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("ddlCity1"), DropDownList).SelectedValue.ToString
'Type of binding
Dim myBinding As New BasicHttpBinding
'Endpoint name defining
Dim myEndPoint As New EndpointAddress(ConfigurationManager.AppSettings("CustomerServiceUrl").ToString())
Dim myChannelFactory As ChannelFactory(Of ICustomer) = New ChannelFactory(Of ICustomer)(myBinding, myEndPoint)
'Creating a Channel to access Services
Dim client As ICustomer = myChannelFactory.CreateChannel
result = client.UpdateCustomerDetails(CustObj)
grdCustomers.EditIndex = -1
GetAllCustomersData()
Sub End
取消更新:
Protected Sub grdCustomers_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
grdCustomers.EditIndex = -1
GetAllCustomersData()
End Sub
答案 2 :(得分:0)
将显示编辑命令按钮,从后端执行此操作。
这是我的网格:
<asp:GridView runat="server" AutoGenerateColumns="false" ID="grdCustomers" DataKeyNames="CustomerID" AllowSorting="true"
OnRowEditing="grdCustomers_RowEditing" OnRowDeleting="grdCustomers_RowDeleting" HeaderStyle-BackColor = "green"
OnRowUpdating="grdCustomers_RowUpdating" OnRowCancelingEdit="grdCustomers_RowCancelingEdit" AllowPaging="false" ShowFooter="true" >
<Columns>
<asp:TemplateField HeaderText="Customer Id">
<ItemTemplate>
<asp:Label Text='<%#Eval("CustomerID")%>' HeaderText="" runat="server" ID="lblcustomerId" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Name">
<ItemTemplate>
<asp:Label Text='<%#Eval("ContactName")%>' ID="lblCustomerName" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContactName" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label id="lblCity" Text='<%#Eval("City")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCity1" runat="server" DataTextField="City" DataValueField="City"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
</Columns>
</asp:GridView>
我的后端代码绑定数据:
Protected Sub GetAllCustomersData()
' List to Hold the results
'Type of binding
Dim myBinding As New BasicHttpBinding
'Endpoint name defining
Dim myEndPoint As New EndpointAddress(ConfigurationManager.AppSettings("CustomerServiceUrl").ToString())
'Registering Service Reference
Dim MychannelFactory As ChannelFactory(Of ICustomer) = New ChannelFactory(Of ICustomer)(myBinding, myEndPoint)
'Creating a Channel to access Services
Dim client As ICustomer = MychannelFactory.CreateChannel
lst = client.GetCustomerData()
grdCustomers.DataSource = lst
grdCustomers.DataBind()
End Sub
对于行编辑:
Protected Sub grdCustomers_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdCustomers.RowEditing
grdCustomers.EditIndex = e.NewEditIndex
GetAllCustomersData()
End Sub
对于行更新:
受保护的子grdCustomers_RowUpdating(发件人为对象,e为GridViewUpdateEventArgs) 昏暗的结果作为布尔值 昏暗的CustObj作为新的Services.Entities.Customers.Customer CustObj.CustomerID = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl(“lblCustomerId”),Label).Text CustObj.ContactName = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl(“txtContactName”),TextBox).Text CustObj.City = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl(“ddlCity1”),DropDownList).SelectedValue.ToString
'Type of binding
Dim myBinding As New BasicHttpBinding
'Endpoint name defining
Dim myEndPoint As New EndpointAddress(ConfigurationManager.AppSettings("CustomerServiceUrl").ToString())
Dim myChannelFactory As ChannelFactory(Of ICustomer) = New ChannelFactory(Of ICustomer)(myBinding, myEndPoint)
'Creating a Channel to access Services
Dim client As ICustomer = myChannelFactory.CreateChannel
result = client.UpdateCustomerDetails(CustObj)
grdCustomers.EditIndex = -1
GetAllCustomersData()
End Sub
对于行编辑取消按钮:
Protected Sub grdCustomers_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
grdCustomers.EditIndex = -1
GetAllCustomersData()
End Sub