1

I'm experiencing some trouble accessing the value of an asp element from code behind.

Basically, In my .ascx file I have a repeater and a data source. The repeater puts data from the data source (which is a database) into an HTML table like so:

<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource1">
    <HeaderTemplate>
        <table id="VersionsTable">

    </HeaderTemplate>

    <ItemTemplate>
        <thead>
        <tr>
            <th id="id">ID</th>
            <th id="date">Date</th>
            <th id="name">Name</th>
            <th id="message">Message</th>
            <th id="delete">Delete</th>
        </tr>
    </thead>
    <tbody>
        <tr>
          <td> <asp:TextBox ID="idfield" runat="server" value='<%# Eval("LeaveAMessageID") %>' /></td>
            <td id="idfield1"><%# Eval("LeaveAMessageID") %></td>
            <td id="datefield"><%# Eval("FormInserted") %></td>
            <td id="namefield"><%# Eval("TextBoxControl") %></td>
            <td id="messagefield"><%# Eval("TextAreaControl") %></td>
            <td id="deletebutton"><asp:Button id="Button1" Text="Delete Message" onclick="Button1_Click" 
             runat="server"> </asp:Button></td>
        </tr>
    </tbody>
    </ItemTemplate>

    <FooterTemplate>
        </table>
    </FooterTemplate>

</asp:Repeater>

Now, what i need to be able to do is to delete a table row from the database based on the value in the textbox with the id "idfield".

So when the button in the last column is clicked, the record will be deleted from the database.

The repeater and the table works as expected, they produce the output that i want. However, i can't seem to access the asp:textbox element from the code behind file.

My Button1_Click method looks like this:

 public void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //TextBox f = (TextBox)this.FindControl("idfield");
            //var msgID = /*Request.Form["idfield"];idfield.Value;*/Request["idfield"];
            //var msgID = Request.QueryString["idfield"];
            //var msgID = DataBinder.Eval(this, "idfield"); 
            var control = (WebUserControl2)LoadControl("~/Controls/WebUserControl2.ascx");
            var msgID = control.Repeater2.FindControl("idfield");
            //var msgID = this.FindControl("Repeater2", 0).FindControl("idfield", 0);



            //Connect to database

After this section of code follows the database connection and the execution of the SQL statement. Everything that you see outcommented in the code section above is the things I have tried to access the value of that asp:TextBox element, and with no success so far.

So my question is: How do i access the asp:TextBox element from code behind?

1 Answer 1

1

Not exactly what you asked but I think this is what you are looking for. Buttons have a CommandArgument property which can be used to store the id of the record you want to delete. This can be added to your button like so:

<asp:Button id="Button1" Text="Delete Message" onclick="Button1_Click" 
         runat="server" CommandArgument="<%# Eval("LeaveAMessageID") %>">

And can then be accessed from you click event like so:

public void Button1_Click(object sender, EventArgs e)
{
    var id = ((Button)sender).CommandArgument;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you rdans, this worked like a charm! I know it wasn't exactly what I asked for, but it did solve my problem, so thanks a million.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.