Search code examples
c#asp.netrepeater

Getting value from textarea in ASP.NET repeater?


I have an asp.net repeater control, displaying data from a sql db. I have delete, edit and save buttons displaying next to each record, sort of like an admin.

When edit is clicked, hidden textboxes appear in place of the literals that were displaying the data. When save is clicked, the new values should be saved to the db (which they are), and the literals display the new value.

But the new value strings are returning as empty strings. They're not null, but the string contains no characters!

This worked fine when I had two TextBox's , however when I switched one to a HTMLTextArea , this happened!

Here is my relevant code...

if (e.CommandName == "edit")
    {
        ((TextBox)e.Item.FindControl("Onebox")).Visible = true;
        ((TextBox)e.Item.FindControl("Onebox")).Text = ((Literal)e.Item.FindControl("onelit")).Text;
        ((HtmlTextArea)e.Item.FindControl("Threebox")).Visible = true;
        ((HtmlTextArea)e.Item.FindControl("Threebox")).Value = ((Literal)e.Item.FindControl("threelit")).Text;
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();



        ((Literal)e.Item.FindControl("onelit")).Visible = false;
        ((Literal)e.Item.FindControl("threelit")).Visible = false;

        ((LinkButton)e.Item.FindControl("LinkButton2")).Visible = false;
        ((LinkButton)e.Item.FindControl("LinkButton3")).Visible = true;



    }

    if (e.CommandName == "save")
    {     

        string newoneval = ((TextBox)e.Item.FindControl("Onebox")).Text;
        string newtwoval = ((HtmlTextArea)e.Item.FindControl("Threebox")).Value;

        ((TextBox)e.Item.FindControl("Onebox")).Visible = false;
        ((Literal)e.Item.FindControl("onelit")).Text = ((TextBox)e.Item.FindControl("Onebox")).Text;
        ((HtmlTextArea)e.Item.FindControl("Threebox")).Visible = false;
        ((Literal)e.Item.FindControl("threelit")).Text = ((HtmlTextArea)e.Item.FindControl("Threebox")).Value;


        ((Literal)e.Item.FindControl("onelit")).Visible = true;
        ((Literal)e.Item.FindControl("threelit")).Visible = true;

        ((LinkButton)e.Item.FindControl("LinkButton2")).Visible = true;
        ((LinkButton)e.Item.FindControl("LinkButton3")).Visible = false;


        if(newoneval != null && newtwoval != null)
        {
            SqlDataReader dataReader;
            String editstr = "update news set title = '" + newoneval + "',  short_desc ='" + newtwoval + "' where pk_ID = @pk_ID";
            SqlCommand command = new SqlCommand(editstr, conn);
            command.Parameters.AddWithValue("@pk_ID", e.CommandArgument);
            try
            {
                conn.Open();
                dataReader = command.ExecuteReader();
                dataReader.Close();
                command.Dispose();
                conn.Close();
                BindRepeater();
            }
            catch (Exception exc)
            {
                Response.Write(exc);
            }

        }
        else
        {
            Response.Write("No value");
        }

and here is the asp repeater template...

<asp:Repeater ID="list_holder" runat="server" OnItemCommand="runCommands">
        <ItemTemplate>
            <table>
                <tr>
                    <!-- FIRST BOX CONTROLS ---------------->
                    <td class="cells">
                        <asp:Textbox id="Onebox" 
                            text=''
                            runat="server"
                            enabled="true"
                            visible="false">
                        </asp:Textbox>

                        <asp:Literal id="onelit"
                            runat="server"
                            text='<%# DataBinder.Eval(Container.DataItem ,"title") %>'
                         />
                    </td>

                    <td class="cells">
                        <textarea id="Threebox"
                            text=''
                            runat="server"
                            enabled="true"
                            Visible="false">
                        </textarea>

                        <asp:Literal id="threelit"
                            text='<%# DataBinder.Eval(Container.DataItem ,"short_desc") %>'
                            runat="server"
                        />
                    </td>
                    <!-------------------------------------->

                    <!---- Link Buttons ------------------------>
                    <td class="cells">
                        <asp:LinkButton ID="LinkButton1"
                            runat="server" 
                            CommandName="delete" 
                            OnClientClick='javascript:return confirm("Are you sure you want to delete?")' 
                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "pk_ID") %>'
                            CausesValidation="false">Delete</asp:LinkButton>  
                    </td>

                    <td class="cells">
                        <asp:LinkButton ID="LinkButton2"
                            runat="server"
                            CommandName="edit"  
                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "pk_ID") %>'
                            CausesValidation="false"
                            Visible ="true">Edit</asp:LinkButton> 

                        <asp:LinkButton ID="LinkButton3"
                            runat="server"
                            CommandName="save"  
                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "pk_ID") %>'
                            CausesValidation="false"
                            Visible ="false">Save</asp:LinkButton>
                    </td>
                    <!-------------------------------------------------->
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>

Why are both the strings returning empty, when before they were returning correctly?


Solution

  • In ASP.NET when you set visible to false, Control don't render on page.

    For more see MSDN.