当我单击 LinkButton 时,我试图从 TextBox 中检索文本值,这两者都在 DataList 的 ItemTemplate 中。但它返回一个空值。
这是aspx页面中datalist的代码:
<asp:DataList ID="DataList1" runat="server" Width="100%" DataKeyField="UserId" OnItemCommand="itemcommand"
OnItemDataBound="Data_Bind">
<ItemTemplate>
<table style="width: 100%">
<tr>
<td>
<a href="<%#System.String.Format("ViewProfile.aspx?aid={0}",Eval("UserId")) %>">
<asp:Image ID="Img" Width="100px" Height="80px" BorderStyle="Inset" BorderWidth="2px"
BorderColor="Aqua" runat="server" ImageUrl='<%#System.String.Format("../Content/Upload/{0}", Eval("UserDetail.UserPhoto")) %>' />
</td>
<td >
<asp:Label ID="name" runat="server" Text='<%#Eval("UserDetail.Firstname")%>'></asp:Label>
<asp:Label ID="lastname" runat="server" Text='<%#Eval("UserDetail.Lastname")%>'></asp:Label>
</td>
<td>
<asp:Label ID="post" runat="server" Text='<%#Eval("Post.post")%>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:LinkButton ID="Like" runat="server" CommandName="onLike" CommandArgument='<%# Eval("Post.PostId") %>'>Like</asp:LinkButton>
<asp:Label ID="lk" runat="server"></asp:Label>
<asp:LinkButton ID="Share" runat="server" CommandName="onShare" CommandArgument='<%# Eval("Post.PostId") %>'>Share</asp:LinkButton>
<asp:Label ID="sh" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:TextBox ID="txtcomment" runat="server" Height="40px" Text="abc" Width="600px"></asp:TextBox>
<input type="text" id="cmnt1" name="cmnt1" runat="server" />
<asp:LinkButton ID="Comment" runat="server" CommandName="onComment" CommandArgument='<%# Eval("Post.PostId") %>'>Comment</asp:LinkButton>
</td>
</tr>
<tr>
<td colspan="3">
<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="Img2" Width="50px" Height="40px" BorderStyle="Inset" BorderWidth="2px"
BorderColor="Aqua" runat="server" ImageUrl='<%#System.String.Format("../Content/Upload/{0}", Eval("UserDetail.UserPhoto")) %>' />
<asp:Label ID="fname" runat="server" Text='<%#Eval("UserDetail.Firstname")%>'></asp:Label>
<asp:Label ID="lname" runat="server" Text='<%#Eval("UserDetail.Lastname")%>'></asp:Label>
</td>
<td>
<asp:Label runat="server" ID="cmnts" Text='<%#Eval("Comments.Comment")%>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
这是背后的代码:
protected void itemcommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "onLike")
{
var k = e.CommandArgument;
User ObjUser = new User();
ObjUser = (User)Session["user"];
ObjUser.Post.PostId = Convert.ToInt64(k);
PostService.OnLike(ObjUser);
DataList1.DataSource = PostService.GetPosts(ObjUser);
DataList1.DataBind();
}
if (e.CommandName == "onComment")
{
var k = e.CommandArgument;
TextBox cmnt = (TextBox) e.Item.FindControl("txtcomment");
HtmlInputText cmnt1 = (HtmlInputText)e.Item.FindControl("cmnt1");
User ObjUser = new User();
ObjUser = (User)Session["user"];
ObjUser.Post.PostId = Convert.ToInt64(k);
ObjUser.Comments.Comment = cmnt.Text;
PostService.OnComment(ObjUser);
DataList1.DataSource = PostService.GetPosts(ObjUser);
DataList1.DataBind();
}
if (e.CommandName == "onShare")
{
var k = e.CommandArgument;
User ObjUser = new User();
ObjUser = (User)Session["user"];
ObjUser.Post.PostId = Convert.ToInt64(k);
PostService.OnShare(ObjUser);
DataList1.DataSource = PostService.GetPosts(ObjUser);
DataList1.DataBind();
}
}