c# - Bound value to a hiddenfield inside a datalist is not specific to an item -


i'm beginner asp.net(c#) , i'm working on social networking application. i've stuck on status update , comment section. want when page loaded threads(status') should have ids in hiddenfields inside datalist i've created itemtemplate status , assigned thread_id hiddenfield. problem hiddenfield not having different values different items. don't know i'm doing mistake. please me.

here code datalist:

<asp:datalist id="dataliststatus" runat="server" repeatcolumns="1"             onitemdatabound="dataliststatus_itemdatabound"             onitemcommand="dataliststatus_itemcommand">         <itemtemplate>             <table width="550px">                 <tr><asp:hiddenfield id="hfieldthreadid" value='<%#eval("thread_id") %>' runat="server" />                     <td style="vertical-align:top; width:50px;" align="left" rowspan="3"><a href='userprofile.aspx?loginid=<%#eval("regid")%>' ><img alt="propic" src="../images/propic.jpg" width="50px" height="50px" /></a></td>                     <td style="vertical-align:top; width:250px;" align="left"><a href='userprofile.aspx?loginid=<%#eval("regid")%>' ><%#eval("regid")%></a></td>                     <td style="vertical-align:top; width:250px;" align="right"><%#eval("st_time") %></td>                 </tr>     </table>   </itemtemplate> </asp:datalist> 

and here code behind:

protected void page_load(object sender, eventargs e) {     if (!ispostback)     {         da.selectcommand = new sqlcommand("select * status regid='" + session["user"].tostring() + "'", con);         con.open();         da.fill(ds, "status");         con.close();         dataliststatus.datasource = ds.tables["status"];         dataliststatus.databind();     }  } 

and here i'm using hiddenfield's value in inner datalist comments:

protected void dataliststatus_itemdatabound(object sender, datalistitemeventargs e) {     da.selectcommand = new sqlcommand("select * comment thread_id='" +      ((hiddenfield)e.item.findcontrol("hfieldthreadid")).value + "'", con);     con.open();     da.fill(ds, "cmts");     con.close();     ((datalist)e.item.findcontrol("datalistcmt")).datasource = ds.tables["cmts"];     ((datalist)e.item.findcontrol("datalistcmt")).databind(); } 

one thing notice in itemdatabound want make sure e.item is of type item or alternatingitem before searching , binding controls inside item.

note: please use parameterized query avoid sql injection.

protected void dataliststatus_itemdatabound(object sender, datalistitemeventargs e) {    if (e.item.itemtype == listitemtype.item ||        e.item.itemtype == listitemtype.alternatingitem)    {     da.selectcommand = new sqlcommand("select * comment thread_id='" +      ((hiddenfield)e.item.findcontrol("hfieldthreadid")).value + "'", con);     con.open();     da.fill(ds, "cmts");     con.close();     var datalist = (datalist)e.item.findcontrol("datalistcmt");     datalist.datasource = ds.tables["cmts"];     datalist.databind();    } } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -