繁体   English   中英

从ASP.NET中继器中的textarea获取价值?

[英]Getting value from textarea in ASP.NET repeater?

我有一个asp.net转发器控件,显示来自sql db的数据。 我在每条记录旁边都显示有删除,编辑和保存按钮,有点像管理员。

单击edit ,隐藏的文本框将代替显示数据的文字出现。 单击“ save ,新值应保存到db中(它们是),并且文字将显示新值。

但是新值字符串将作为空字符串返回。 它们不为null,但字符串不包含任何字符!

当我有两个TextBox时,这很好用,但是当我将一个切换到HTMLTextArea ,发生了这种情况!

这是我的相关代码...

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");
        }

这是ASP中继器模板...

<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>

为什么在正确返回之前,这两个字符串都返回空?

在ASP.NET中,当将visible设置为false时,Control不在页面上呈现。

有关更多信息, 请参见MSDN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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