0

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

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. 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>");
        }     
1
  • Try, DeleteButton.CommandArgument=name; in Page_Load. Commented Jun 12, 2012 at 10:03

2 Answers 2

3

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; 
   } 
} 
Sign up to request clarification or add additional context in comments.

1 Comment

The <%# Eval("UserName")%> does not work. when carried forward to the code behind nothing at all prints... just the hardcoded '--'
1

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

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

EDIT

Aren't you using Data control like GridView, FormView or DataList? if not you have to use one of those in order to use Eval method. As a example of GridView you have to bind GridView datasource first.

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?

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.

1 Comment

Its just showing a blank. Do i need to do something before i use Eval

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.