单击按钮时,文本将显示在文本框中

时间:2013-11-18 14:51:13

标签: c# asp.net textbox linkbutton

我有一些文字要显示在文本框中,我点击链接按钮“编辑” 如您所见,我在这些链接按钮上有CommandNamesCommandArguments所以我需要一个可以在代码隐藏(.cs)中编写的解决方案,因为我得到if (e.CommandName == "Edit")

我没有尝试任何东西,因为我无法找到任何关于它的东西,也不在我脑海里。

设计师代码:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater1_ItemCommand">
    <asp:ItemTemplate>
        <tr>
            <td>
                <asp:TextBox ID="TextBox_Text" runat="server" Text='<%# Eval("Text") %>'>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton_Save" runat="server" CommandName="Save" CommandArgument='<%# Eval("Id") %>'> Save </asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton_Edit" runat="server" CommandName="Edit" CommandArgument='<%# Eval("Id") %>'> Edit </asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton_Delete" runat="server" CommandName="Delete" CommandArgument='<%# Eval("Id") %>'> Delete </asp:LinkButton>
            </td>
        </tr>
    </asp:ItemTemplate>
</asp:Repeater>

<asp:SqlDataSource runat="server" ID="SqlDataSource_Forside" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [Table1]"> <asp:SqlDataSource>


代码背后:


if (e.CommandName == "Edit")
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString =
            ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "Select * FROM Table1 WHERE Id = @Id";


        cmd.Parameters.Add("@Id", SqlDbType.Int).Value = e.CommandArgument.ToString();

        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();

        if (reader.Read())
        {
            TextBox_Edit.Text = reader["Text"].ToString(); // i dont have the textbox yet..
        }
        conn.Close();

        Repeater1.DataBind();

    }

2 个答案:

答案 0 :(得分:0)

你可以使用OnRowCommand使用DataGridView ..这样的东西:

protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "Delete")
        {
           id = Convert.ToInt32(gridName.DataKeys[Convert.ToInt32(e.CommandArgument)]["id_DataKey"].ToString());
           TextBox txt = (TextBox)e.Row.FindControl("txtBoxName");
           txt.Text = "Removed";
        }
}

答案 1 :(得分:0)

这就是你要做的事情:

if (e.CommandName == "Edit")
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString =
        ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "Select * FROM Table1 WHERE Id = @Id";


    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = e.CommandArgument.ToString();

    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();


    TextBox_Text.Text=reader["Text"].ToString();;

    conn.Close();

    Repeater1.DataBind();

}