我在一个可扩展的GridView上的templateField中有一个按钮,我需要按下按钮,将同一模板字段中的文本框内容保存到数据库中,然后再显示文本框中的文本。
类似的东西:
在textBox中输入文字 - >保存 - >将文本从TextBox发送到DB - >显示保存在TextBox中的文本
我用来定义可扩展gridview的标记代码如下:
<script language="javascript" type="text/javascript">
function divexpandcollapse(divname) {
var div = document.getElementById(divname);
var img = document.getElementById('img' + divname);
if (div.style.display == "none") {
div.style.display = "block"; img.src = "Images/Icons/minus.jpg";
} else { div.style.display = "none"; img.src = "Images/Icons/plus.jpg"; }
}</script>
<asp:GridView ID="GV_SL" runat="server" OnRowDataBound="gvUserInfo_RowDataBound"
DataSourceID="SQL" >
<%-- Style="font-size: x-small" AllowPaging="True" --%>
<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("ID") %>');">
<img id="imgdiv<%# Eval("ID") %>" width="15px" border="0" src="Images/Icons/plus.jpg" /></a></ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>
<asp:BoundField DataField="Value" HeaderText="Value" SortExpression="Value">
<ItemStyle HorizontalAlign="Left" Font-Bold="True" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("reporting_group") %>" style="display: none; position: relative;
left: 15px; overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" />
<Columns>
<asp:BoundField DataField="Value2" HeaderText="Value2" HeaderStyle-HorizontalAlign="Left"
DataFormatString="{0:N0}" />
</Columns>
</asp:GridView>
<br />
<asp:TextBox ID="TB_Comments" runat="server" Text="Example: Text will be entered here"
TextMode="MultiLine" Rows="4" Width="510px"></asp:TextBox>
<asp:Button ID="B_Save" runat="server" CommandName="AddText"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Save Changes" />
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
目前我有两个问题:
我为第二点尝试了以下代码,并将测试消息加载到标签和textBox中,但它似乎没有做任何事情:
Protected Sub GV_SL_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "AddText") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button
' from the Rows collection.
Dim row As GridViewRow = GV_SL.Rows(index)
Label1.Text = index & " - test"
' Add code here to add the item to the shopping cart.
Dim TB_Com_Control As System.Web.UI.WebControls.TextBox = DirectCast(row.FindControl("TB_Comments"), System.Web.UI.WebControls.TextBox)
TB_Com_Control.Text = "Test "
End If
End Sub
有什么想法吗?
感谢
答案 0 :(得分:0)
我设法解决了这两个问题
1.每次按下项目模板内的按钮,展开的行都会缩小
我通过添加AJAX更新面板解决了这个问题
<asp:UpdatePanel ID="UP_Text" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TB_Comments" runat="server" Text="Example: Text will be entered here"
TextMode="MultiLine" Rows="4" Width="510px"></asp:TextBox>
<asp:Button ID="B_Save" runat="server" CommandName="AddText"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Save Changes" />
</ContentTemplate>
</asp:UpdatePanel>
2.按下按钮后,我无法创建事件。
我通过添加OnRowCommand =“GV_SL_RowCommand”
解决了这个问题 <asp:GridView ID="GV_SL" runat="server" OnRowDataBound="gvUserInfo_RowDataBound"
DataSourceID="SQL" OnRowCommand="GV_SL_RowCommand" >
希望有所帮助