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