难以将标签值传递给列表视图
本文关键字:列表 视图 值传 标签 | 更新日期: 2023-09-27 17:53:57
我要找的是获得"Label10"的值,这是进入viewmsgView,然后根据标签值绑定FormView1,注意:viewmsgView &FormView1位于同一视图和同一页面中。但我得到一个错误消息如下,我已经添加了查找控制,因为它给出的错误消息,控制不可用或不存在。
"对象引用未被设置为对象的实例"
RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@mailno", ((Label)myControl).Text);
viewmsgView,其中包括Label10以及titlehyplink,当用户点击它将显示并绑定FormView1
<asp:ListView ID="viewmsgView" runat="server">
<ItemTemplate>
<div class="col-lg-12">
<table class="table table-inbox table-hover" style="width:100%; margin-left:0px;">
<tbody>
<tr class="unread">
<td>
<asp:ImageButton ID="deltimgbtn" runat="server"
ImageUrl="~/iconsimg/Delete2.png" onclick="deltimgbtn_Click" /> </td> <td>
<asp:ImageButton ID="ReplyBtn" runat="server" ImageUrl="~/iconsimg/reply.png"
onclick="ReplyBtn_Click" /></td>
<td class="view-message dont-show"><asp:Label ID="msgfromlbl" runat="server" Text='<%# Bind ("sender") %>'></asp:Label></td>
<td class="view-message "><asp:LinkButton
ID="titlehyplink" runat="server" Text='<%# Bind ("Mestitle") %>' onclick="titlehyplink_Click"></asp:LinkButton></td>
<td class="view-message inbox-small-cells"><asp:Label ID="Label10" runat="server" Style="color: #FFFFFF" Text='<%# Bind ("mailno") %>'></asp:Label></td>
<td class="view-message text-right"><asp:Label ID="datelbl" runat="server" Text='<%# Bind ("Date") %>'></asp:Label></td>
</tr></tbody></table>
</div>
</ItemTemplate>
</asp:ListView>
FormView1,它将根据Label10显示消息细节,Label10是viewmsgView
<asp:FormView ID="FormView1" runat="server" Width="100%">
<ItemTemplate>
<br /><br />
<div style="margin-bottom:40px; background-color:#CCCCCC;">
<div class="col-md-4">
<label>From:</label>
<asp:Label ID="senderLabel" runat="server" Text='<%# Bind("sender") %>' />
</div>
<div class="col-md-4">
<label>Date:</label>
<asp:Label ID="DateLabel" runat="server" Text='<%# Bind("Date") %>' />
</div>
<div class="col-md-4">
<label>To:</label>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("Receiver") %>' />
</div>
<div class="col-md-4">
<label>Mail No:</label>
<asp:Label ID="Label15" runat="server" Text='<%# Eval("mailno") %>' />
</div>
<div class="col-md-4">
<label>Ads No:</label>
<asp:Label ID="AdsIDLabel" runat="server" Text='<%# Bind("AdsID") %>' />
</div>
</div>
<br />
<br />
<asp:Label ID="MestitleLabel" runat="server" Text='<%# Bind("Mestitle") %>' Font-Bold="True"
Font-Size="Larger" /><br />
<br />
<asp:Label ID="MessageLabel" runat="server" Text='<%# Bind("Message") %>' Width="100%"
CssClass="labelmsg" />
</ItemTemplate>
</asp:FormView>
标题链接后面的代码
protected void titlehyplink_Click(object sender, EventArgs e)
{
if (Session["UsrNme"] != null)
{
using (SqlConnection RedMsgSQLCon = new SqlConnection(sc))
{
RedMsgSQLCon.Open();
SqlDataAdapter RedMsgDAADP = new SqlDataAdapter(@"SELECT * From mails where Receiver=@UID And mailno=@mailno", sc);
var use = Session["UsrNme"];
Control myControl = FindControl("Label10");
RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@UID", use);
RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@mailno", ((Label)myControl).Text);
DataSet RedMsgCVs = new DataSet();
RedMsgDAADP.Fill(RedMsgCVs);
FormView1.DataSource = RedMsgCVs.Tables[0];
FormView1.DataBind();
FormView1.Visible = true;
}
}
else
{
}
}
Label10控件是viewmsgView中的子控件,将为每一行创建一个唯一的ID,您将拥有与viewmsgView中的行一样多的Label10控件。一种不费多大力气就能获取值的方法是修改以下行:
<asp:LinkButton ID="titlehyplink" runat="server" Text='<%# Bind ("Mestitle") %>' onclick="titlehyplink_Click"></asp:LinkButton>
这:
<asp:LinkButton ID="titlehyplink" runat="server" Text='<%# Bind "Mestitle") %>' CommandArgument='<%# Bind ("mailno") %>' OnCommand="titlehyplink_Command"></asp:LinkButton>
,然后在你的代码更改
protected void titlehyplink_Click(object sender, EventArgs e)
protected void titlehyplink_Command(object sender, CommandEventArgs e)
在这个方法中,你可以这样使用:
RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@mailno", e.CommandArgument);
几个技巧:
FindControl("Label10");只查看Page控件集合,而不是子控件集合。
Label10的Id将自动生成,并在其末尾添加一个数字,如Label10_0,并且还包含父控件的Id,如viewmsgView_Label10_0,因此您无法仅用"Label10"找到它