将变量从asp:imagebutton传递给代码隐藏方法

时间:2012-06-12 12:24:04

标签: c# asp.net .net

  

可能重复:
  passing a paramater from a ImageButton to code behind

我想将一个变量(在这种情况下,包含字符串bill的'name')传递给代码隐藏方法并使用debug.print来查看它

string name = "bill";
<asp:ImageButton runat="server" id="DeleteButton"  ImageUrl="Images/delete.jpg" 
CommandArgument='<%# Eval("name") %>' CommandName="ThisBtnClick" text="Delete Me" onclick="DeleteMonth" /> 

我试过了:

CommandArgument='<%# Eval("name") %>'
CommandArgument='<%# Bind("name") %>'
CommandArgument='<%= "name" %>

这是我的打印功能

protected void DeleteMonth(object sender, EventArgs e)
{
    ImageButton btn = (ImageButton)sender;
      switch (btn.CommandName)
    {
        case "ThisBtnClick":
            Debug.Print("--" + btn.CommandArgument.ToString());
            break;
        case "ThatBtnClick":
            break;
    }
}

我想我需要首先对数据进行数据处理,但老实说,我不知道。我打印时只是一片空白。有没有人在此之前处理过这个问题

修改

我想要做的是动态创建一个表。 name变量从数据库中提取,然后根据该名称创建一个表。我希望表的最后一列是X,所以我可以删除该行中的所有内容。我正在使用图像按钮。这是我的代码

using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(sqlConnectionString))
    {
        conn.Open();
        string query = "SELECT * FROM dbo.Archive";
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, conn);
        System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            string name, created, 1Updated, 2Updated, 3Updated;
            name = rdr.GetString(1);                    // name of month
            if (!(rdr.IsDBNull(2)))                     // checks date for a null value
            { created = rdr.GetDateTime(2).ToString(); }// if not null date is turned into a string and saved in a variable
            else
            { created = "---"; }                        // else date is given a default value
            if (!(rdr.IsDBNull(3)))
            { 1Updated = rdr.GetDateTime(3).ToString(); }
            else
            { 1Updated = "---"; }
            if (!(rdr.IsDBNull(4)))
            { 2Updated = rdr.GetDateTime(4).ToString(); }
            else
            { 2Updated = "---"; }
            if (!(rdr.IsDBNull(5)))
            { 3Updated = rdr.GetDateTime(5).ToString(); }
            else
            { 3Updated = "---"; }

            Response.Write("<tr><td>" + name + "</td>");
            Response.Write("<td>" + created + "</td>");
            Response.Write("<td>" + 1Updated + "</td>");
            Response.Write("<td>" + 2Updated + "</td>");
            Response.Write("<td>" + 3Updated + "</td>");
            Response.Write("<td><a>1</a></td>");
            Response.Write("<td><a>2</a></td>");
            Response.Write("<td><a>3</a></td>");
            Response.Write("<td><a>Compliance Summary</a></td>");

            Response.Write("<td align = 'center'>"+name);%> 
            <asp:ImageButton runat="server" id="DeleteButton"  ImageUrl="Images/delete.jpg" 
            CommandArgument='<%# Eval("name") %>' CommandName="ThisBtnClick" text="Delete Me" onclick="DeleteMonth" /> 
            <% Response.Write("</td></tr>");
        }     

2 个答案:

答案 0 :(得分:1)

您需要在代码隐藏中设置属性,而不是使用变量:

DeleteButton.CommandArgument = whatever;

答案 1 :(得分:0)

试试这个

CommandArgument='<%= GetName() %>'

&安培;在Code背后

Public string GetName()
{
 return "Bill";
}