我在UpdatePanel中有一个GridView,可以触发RowCommand事件。在触发RowCommand事件时,我将e.CommandArgument值分配给标签,但它不显示效果。我在执行期间在事件上设置了断点,它设置了标签文本属性,但在退出事件后,标签丢失了它的值并转到了之前的文本。我将值存储在ViewState和Session中但仍然无法正常工作。以下是我的代码。
这里的GridView1_RowCommand事件想要赋值给lblValue这是问题
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
lblValue.Text = e.CommandArgument.ToString();
}
}
这里我想通过标签值
从数据库中删除记录 protected void Button2_Click(object sender, EventArgs e)
{
Int32 id = Convert.ToInt32(lblValue.Text);
conn.RegionalBusinessUnits.Remove(conn.RegionalBusinessUnits.Where(rbu => rbu.Id == id).FirstOrDefault());
conn.SaveChanges();
}
这是标记
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div class="BoxStyle" >
<div class="header"><asp:HyperLink ID="hlBack" runat="server"><img src="../images/back-icon.png" alt="Go Back" height="20" width="20" style="vertical-align: middle; text-align: center; cursor: pointer;" /></asp:HyperLink> Regional Department Unit List</div>
<div id="myModal" class="reveal-modal">
<h1>Delete</h1>
<p>This will guide you through the delete process</p>
<asp:Label ID="lblValue" runat="server" Text="Label"></asp:Label>
<p><asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /></p>
<a class="close-reveal-modal">×</a>
</div>
<div class="contents">
<center>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="RBUEntityDataSource" CssClass="gridview" OnRowCommand="GridView1_RowCommand" EnableViewState="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Region" HeaderText="Region" ReadOnly="True" SortExpression="Region" />
<asp:TemplateField HeaderText="Update">
<ItemTemplate>
<asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" OnClick="lbUpdate_Click">Update</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lbDelete" class="big-link" data-reveal-id="myModal" runat="server" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' OnClick="lbDelete_Click">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="RBUEntityDataSource" runat="server" ConnectionString="name=ChemonicsDBEntities" DefaultContainerName="ChemonicsDBEntities" EnableFlattening="False" EntitySetName="RegionalBusinessUnits" Select="it.[Id], it.[Region]" OrderBy="it.[Id] asc" Where="it.[DeletedBy] = 0">
</asp:EntityDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</center>
</div>
<div class="bottom"></div>
</div>
答案 0 :(得分:0)
lblValue没有更新,因为它在UpdatePanel1之外。如果将带有ContentTemplate的UpdatePanel1直接移到ScriptManager下,它应该可以工作。
一定要确定 </ContentTemplate>
</asp:UpdatePanel>
在最底层。
然后,您可以删除触发器并将UpdatePanel“UpdateMode”更改为“Always”
答案 1 :(得分:0)
<asp:GridView runat="server" ID="gvDocuments" AutoGenerateColumns="false"
AllowPaging="true" PageSize="10" DataKeyNames="DocumentID">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnDocumentViewAttachmentInGrid" runat="server" Text="View Attachment" CommandName="ViewAttachment" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我的按钮单击我试图将文件流式传输到客户端,但得到错误“Microsoft JScript运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。”因为我在更新面板上。我尝试注册datagridview进行回发,没有任何效果。最后,在代码中,我尝试了这个:
Protected Sub gvDocuments_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDocuments.RowCreated
Try
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim btnDocumentViewAttachmentInGrid As Button = e.Row.FindControl("btnDocumentViewAttachmentInGrid")
btnDocumentViewAttachmentInGrid.CommandArgument = e.Row.RowIndex.ToString()
ScriptManager1.RegisterPostBackControl(btnDocumentViewAttachmentInGrid)
End If
Catch ex As Exception
'handle error here
End Try
End Sub
...然后我可以像这样传输文件:
Protected Sub gvDocuments_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvDocuments.RowCommand
Try
If e.CommandName = "Select" Then
gvDocuments.SelectedIndex = e.CommandArgument
ElseIf e.CommandName = "ViewAttachment" Then
Dim iDocumentID As Int64 = gvDocuments.DataKeys(e.CommandArgument).Value
Dim oRows() As DataRow = dtDocs.Select("DocumentID = " & iDocumentID.ToString())
If oRows.Length > 0 Then
Dim oRow As DataRow = oRows(0)
Dim strFileName As String = oRow("NetworkPath").ToString()
Dim strJustTheName As String = oRow("DocumentID").ToString() & "." & oRow("FileNameExtension").ToString()
strFileName &= strJustTheName
Response.Clear()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=""{0}""", strJustTheName))
Response.TransmitFile(strFileName)
Response.Flush()
Response.End()
End If
End If
Catch ex As Exception
'handle error
End Try
End Sub
......和BOOM去炸药,完全奏效! :)
答案 2 :(得分:0)
在这种情况下不是,但是可能是您向具有相同ID的GridView添加控件,您必须具有唯一的ID。 我遇到了这个问题,并且有效
答案 3 :(得分:0)
在Page_Load上必须像这样重新绑定gridview
If (!Page.IsPostBack)
{
bindhere()
}