我在可扩展的 GridView 上的 templateField 中有一个按钮,当按下按钮时,我需要将同一模板字段中的文本框的内容保存到数据库中,并再次显示文本框中的文本。
就像是:
在 textBox 中输入文本 -> 保存 -> 将文本从 TextBox 发送到 DB -> 显示保存在 TextBox 中的文本
我用来定义可扩展网格视图的标记代码如下:
<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>
我目前有两个问题:
- 每次我按下 itemtemplate 内的按钮时,展开的行都会折叠
- 按下按钮后,我无法创建事件。
我为第二点尝试了以下代码,并将测试消息加载到标签和文本框中,但它似乎没有做任何事情:
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
有任何想法吗?
谢谢