简体   繁体   中英

How to update text in Textbox in Datalist

This is related to a question I asked about a month ago. I have an html page that loads a Datalist with info about candidates. Each cell has info about the candidate, a Textbox with comments, and a button to submit any changes made to the comments loaded initially. The OnClick method in the corresponding c# code remembers the IdCandidate and the original comments, but I don't know how to make it remember any changes the user makes to the comments Textbox. Thank you. In the HTML:

<asp:Datalist ID="DataList1" runat="server"   >  
 <ItemTemplate>
     <table class="table-candidates">
         <tr>
            <td>
                <%#Eval("IdCandidate")%> 
            </td>
         </tr>
                           
         <tr>
            <td>
               <asp:TextBox ID="Candidate_Comments" runat="server" TextMode="MultiLine" 
                  Height="100px" Width="180px" Text='<%#Bind("CandidateComments")%>' ></asp:TextBox>
            </td>
         </tr>

         <tr>
            <td>
               <asp:button ID="BtnSaveComments" runat="server" text="Save Comments" 
               CommandArgument='<%#Eval("IdCandidate") + ";" + Eval("CandidateComments")%>'
                                OnClick="BtnSaveComments_Click" Width="130px" Height="25px" />    
     </table>
 </ItemTemplate>
</asp:Datalist>

In the Codebehind:

protected void BtnSaveComments_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string IdSelected = btn.CommandArgument;
    string[] commandArguments = btn.CommandArgument.Split(';'); 
           //this might not be the best way to send multiple variables
           //to the OnClick method, any suggestions to improve this are also welcome!
    string varIdCandidate = commandArguments[0].ToString();
    string varComments = commandArguments[1].ToString();

    Response.Write("IdCandidate: " + varIdCandidate); //remembers IdCandidate
    Response.Write(“Comments: “ + varComments);  //remembers original comments, but not any changes 
                                                 //typed into the Textbox
    //several processes follow here, including updating SQL database 
    //my question is: how to update the contents of the “Comments” variable
}

You'll need to examine Datalist1.Controls collection to see what is what and where your control is. Then try to Access the Textbox.

Ex:

TextBox tb = (TextBox)DataList1.FindControl("Candidate_Comments");
string varComments = tb.Text;

or this format

Button btn = (Button) sender;
DataListItem item = (DataListItem) btn.NamingContainer;
TextBox txt = (TextBox) item.FindControl("Candidate_Comments");
string varComments =txt.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM