本文介绍了GridView的编辑按钮需要2次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当在GridView控件pressing编辑按钮,显示编辑模板后,才2次点击。
而另一个问题:现场编辑的值最初显示在GridView控件,但不是在编辑模板
天冬氨酸code:
< ASP:GridView控件ID =GridView1=服务器AllowPaging =真AllowSorting =真
的AutoGenerateColumns =假CELLPADDING =4OnRowEditing =EditRow
OnRowCancelingEdit =CancelEditRow的DataKeyNames =AREA的DataMember =默认视图> <柱体和GT;
< ASP:BoundField的数据字段=AREA的HeaderText =AREA只读=真
SORTEX pression =AREA/> < ASP:的TemplateField的HeaderText =LEADER_USERSORTEX pression =LEADER_USER>
<&ItemTemplate中GT;<%#的eval(leader_user)%>< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:文本框ID =txtleaderuser=服务器文本='<%#的eval(leader_user)%>/>
< / EditItemTemplate中>
< / ASP:的TemplateField> < ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:ImageButton的ID =editButton=服务器的CommandName =编辑
的ImageUrl =图片/ pencil1.png文本=编辑工具提示=编辑/>
< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:按钮的ID =BtnUpdate=服务器的CommandName =更新
文本=更新/>
< ASP:按钮的ID =BtnCancel=服务器的CommandName =取消
文本=取消/>
< / EditItemTemplate中>
< / ASP:的TemplateField> < /专栏>
< / ASP:GridView的>
VB code:
保护小组EditRow(BYVAL发件人为对象,BYVAL E上GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
如果不是那么的IsPostBack
GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataBind()
'万一
结束小组保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load SqlDataSource1.SelectCommand =SQL
SqlDataSource1.ConnectionString =我康恩字符串 如果没有的IsPostBack然后
GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
万一
结束小组
解决方案
我认为你需要调用的DataBind
来重新绑定数据源。您已经评价出来,但它看起来像你在的IsPostBack
块,这将仅在初始页面加载执行了吧。
尝试:
保护小组EditRow(BYVAL发件人为对象,BYVAL E上GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
结束小组
When pressing edit button in gridview, edit template is displayed only after 2 clicks.And another problem: Value of the field to edit is displayed in gridview initially, but not in edit template.
Asp code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" OnRowEditing="EditRow"
OnRowCancelingEdit="CancelEditRow" DataKeyNames="AREA" DataMember="DefaultView">
<Columns>
<asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True"
SortExpression="AREA" />
<asp:TemplateField HeaderText="LEADER_USER" SortExpression="LEADER_USER">
<ItemTemplate><%#Eval("leader_user")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtleaderuser" runat="server" Text='<%#Eval("leader_user")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit"
ImageUrl="images/pencil1.png" Text="Edit" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="BtnCancel" runat="server" CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
vb code:
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
'If Not IsPostBack Then
'GridView1.DataSourceID = SqlDataSource1.ID
'GridView1.DataBind()
'End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SqlDataSource1.SelectCommand = "SQL"
SqlDataSource1.ConnectionString = "My conn string"
If Not IsPostBack Then
'GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
End If
End Sub
解决方案
I think you need to call DataBind
to rebind the data source. You've commented it out, but it looks like you had it in a IsPostBack
block, which would only execute on the initial page load.
Try:
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
End Sub
这篇关于GridView的编辑按钮需要2次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!