我有一个datalist控件,显示id,图像,名称和按钮,我想,当我点击按钮时,id应该插入表格.... 我的代码在下面......
.aspx代码
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DataList ID="SQuestionsDataList" class="" runat="server" OnItemCommand="SQuestionsDataList_ItemCommand">
<ItemTemplate>
<div class="thumbnail" style="text-align: left;">
<div class="col-lg-12 thumbnail" style="padding: 0px;">
<div class="col-lg-2">
<asp:Image ID="Image3" class=" img-rounded" ImageUrl='<%# Eval("picture") %>' Width="50px"
Height="50px" Style="" runat="server" />
</div>
<div class="col-lg-10" style="padding: 0px; margin-left: -35px;">
<strong>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("sfname") %>'></asp:Label>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("slname") %>'></asp:Label>
<asp:Label ID="Labelid" runat="server" Text='<%# Eval("Qid") %>'></asp:Label><br />
</strong>Posted On
<asp:Label ID="Label11" runat="server" Text='<%# Eval("SystemDate","{0:D}") %>'></asp:Label>
<asp:Button ID="Button4" runat="server" CommandName="TakeID" Text="Fetch" />
</div>
</div>
<div class="thumbnail" style="text-align: left; margin-top: ; border-bottom-style: none;
width: 100%;">
<div style="padding-top: 5px;">
<asp:Label ID="SQuestions" Font-Size="15px" Style="" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
</div>
<div>
<asp:Image ID="Image2" class="img-rounded" ImageUrl='<%# Eval("Snapshot") %>' Style=""
runat="server" Visible="True" ViewStateMode="Inherit" /><br />
</div>
<div style="color: black; font-size: 15px; padding-top: 5px;">
Like <a href="<%# writeurl(Eval("Qid").ToString() ,Eval("Uid").ToString()) %>"
style="color: black;">Comment </a>
</div>
<div style="color: black; background-color: #222937; color: White; font-size: 15px;
padding-left: 5px;">
0 Likes 0 Comments
</div>
</div>
</div>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
.cs代码
页面加载
if (!IsPostBack)
{
SqlDataAdapter da = new SqlDataAdapter("select * from student full outer join StudentQuestions on student.sid = StudentQuestions.Uid where scourse = '" + Session["scourse"] + "' order by Qid desc", con);
DataTable dt = new DataTable();
da.Fill(dt);
SQuestionsDataList.DataSource = dt;
SQuestionsDataList.DataBind();
}
或其他
protected void SQuestionsDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "call")
{
DataListItem item = (DataListItem)(((Button)(e.CommandSource)).NamingContainer);
string text = ((Label)item.FindControl("Labelid")).Text;
con.Open();
SqlCommand cmd = new SqlCommand("insert into comments (qid , sid) values ('" + text.ToString() + "' , '" + Session["studentid"] + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
感谢您的帮助
答案 0 :(得分:0)
Button的CommandName是CommandName =&#34; TakeID&#34;那么你应该在ItemCommand事件中的if条件中使用相同的CommandName
if (e.CommandName == "TakeID")
{
DataListItem item = (DataListItem)(((Button)(e.CommandSource)).NamingContainer);
string text = ((Label)item.FindControl("Labelid")).Text;
con.Open();
SqlCommand cmd = new SqlCommand("insert into comments (qid , sid) values ('" + text.ToString() + "' , '" + Session["studentid"] + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}