简体   繁体   English

从ImageButton传递参数到后面的代码

[英]passing a parameter from a ImageButton to code behind

EDITED QUESTION 编辑的问题

I want to pass a variable, in this case 'name' containing the string bill, to a code behind method and use debug.print to see it 我想将一个变量(在这种情况下为包含字符串bill的“名称”)传递给方法后面的代码,并使用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" /> 

I've tried: 我试过了:

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

This is my print function 这是我的打印功能

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

I think i need to databind something first but honestly I have no idea. 我认为我需要先进行数据绑定,但是老实说我不知道​​。 I just get a blank when i print. 我在打印时只会一片空白。 Has anyone dealt with this before 有人处理过吗

EDIT 编辑

What I want to do is dynamically create a table. 我想做的是动态创建一个表。 the name variable gets pulled from a database and then creates a table based on that name. 从数据库中提取名称变量,然后根据该名称创建表。 I want the last column of the table to be an X so i can delete everything in that row. 我希望表格的最后一栏为X,以便我可以删除该行中的所有内容。 I'm using an imagebutton. 我正在使用图像按钮。 here's my code 这是我的代码

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

Use like this...... 像这样使用...

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

 protected void DeleteMonth(object sender, EventArgs e)
 {

**ImageButton btn = ((ImageButton)sender).CommandArgument;** 
  switch (btn.ToString()) 
  { 
        case "ThisBtnClick": 
            Debug.Print("--" + btn.CommandArgument.ToString()); 
            break; 
        case "ThatBtnClick": 
            break; 
   } 
} 

Use CommandArgument='<%#Eval("name")%>' 使用CommandArgument='<%#Eval("name")%>'

instead of CommandArgument="<%Response.Write(name);%>" 而不是CommandArgument="<%Response.Write(name);%>"

EDIT 编辑

Aren't you using Data control like GridView, FormView or DataList? 您不使用GridView,FormView或DataList等数据控件吗? if not you have to use one of those in order to use Eval method. 如果不是,则必须使用其中之一才能使用Eval方法。 As a example of GridView you have to bind GridView datasource first. 作为GridView的示例,您必须首先绑定GridView数据源。

http://msdn.microsoft.com/en-us/library/aa479353.aspx http://msdn.microsoft.com/en-us/library/aa479353.aspx

OR 要么

Declare public properties for your DB fields. 声明数据库字段的公共属性。 How can I use Eval in asp.net to read fields from a database? 如何在asp.net中使用Eval读取数据库中的字段?

The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. Eval方法采用数据字段的名称,并从数据源中的当前记录返回一个包含该字段值的字符串。

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

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