Solved

Passing parameter to code behind

Posted on 2016-09-22
17
51 Views
Last Modified: 2016-09-22
Hi,

in the front end, I have the code like this
<asp:Button id="MoveAllbtn" onclick="MoveAllbtn_Click" CustomParameter="listBox1" runat="server" text=">>" />      

Since I want to use the same function, I want to pass the same of the listBox to the function.

In the code behind, I have the code
    protected void MoveAllbtn_Click(object sender, EventArgs e)
    {
       

        foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
        {
              //some codes
        }
        
        string ID=e.CommandArgument.ToString();
	testBox.Text = ID;
        
    }

Open in new window


I am getting error

CS1061: 'System.EventArgs' does not contain a definition for 'CommandArgument' and no extension method 'CommandArgument' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)


any ideas?  thanks
0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
17 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41810792
Convert object sender to button and then get its id

protected void MoveAllbtn_Click(object sender, EventArgs e)
    {
       

        foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
        {
              //some codes
        }
        
       Button moveAllbtn=(Button)sender;
	testBox.Text = moveAllbtn.ID;
        
    }

Open in new window

0
 

Author Comment

by:mcrmg
ID: 41810798
Thanks for the quick reply. I am geting this error:

 CS0104: 'Button' is an ambiguous reference between 'System.Web.UI.WebControls.Button' and 'System.Windows.Forms.Button'
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41810825
This is how you fix that. However, I don't think this code is going to function the way you want.

 
   protected void MoveAllbtn_Click(object sender, EventArgs e)
    {

        foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
        {
            //string iText = item.Text;
        }

        System.Web.UI.WebControls.Button moveAllBtn = (System.Web.UI.WebControls.Button)sender;

        string ID = moveAllBtn.CommandArgument.ToString();
        testBox.Text = ID;
    }

Open in new window


Can you explain more on what you're trying to do?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41810843
Yes, as Daniel said use System.Web.UI.WebControls.Button
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41810852
This will get the custom value you passed:

    protected void MoveAllbtn_Click(object sender, EventArgs e)
    {

        foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
        {
            string iText = item.Text;
        }

        System.Web.UI.WebControls.Button moveAllBtn = (System.Web.UI.WebControls.Button)sender;

        AttributeCollection attributColl = moveAllBtn.Attributes;
        if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
        {
            string myCustomParameterValue = attributColl["CustomParameter"];
        }

        string ID = moveAllBtn.CommandArgument.ToString();
        testBox.Text = ID;
    }

Open in new window

0
 

Author Comment

by:mcrmg
ID: 41810873
I am getting this error  (sorry)

CS0104: 'AttributeCollection' is an ambiguous reference between 'System.Web.UI.AttributeCollection' and 'System.ComponentModel.AttributeCollection'
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41810876
Use System.Web.UI.AttributeCollection there
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41810881
You'd have to cast. Seems strange. You're adding too many using statements.

        System.Web.UI.AttributeCollection attributColl = (System.Web.UI.AttributeCollection)moveAllBtn.Attributes;
        if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
        {
            string myCustomParameterValue = attributColl["CustomParameter"];
        }

Open in new window


Get rid of your Windows using if you can.
1
 

Author Comment

by:mcrmg
ID: 41810894
This is what I have
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
//using System.Windows.Forms;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.AttributeCollection;

public partial class _test2 : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void MoveAllbtn_Click(object sender, EventArgs e)
    {



        foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
        {
            //something
        }

        System.Web.UI.WebControls.Button moveAllBtn = (System.Web.UI.WebControls.Button)sender;

        AttributeCollection attributColl = moveAllBtn.Attributes;
        if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
        {
            string myCustomParameterValue = attributColl["CustomParameter"];
        }

        string ID = moveAllBtn.CommandArgument.ToString();
        testBox.Text = ID;
        
    }

Open in new window

0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41810928
Does it work now?
0
 

Author Comment

by:mcrmg
ID: 41810932
this is the error I am getting

CS0138: A using namespace directive can only be applied to namespaces; 'System.Web.UI.AttributeCollection' is a type not a namespace
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41810935
Remove following line:
using System.Web.UI.AttributeCollection;
0
 

Author Comment

by:mcrmg
ID: 41810942
After removing,

CS0104: 'AttributeCollection' is an ambiguous reference between 'System.Web.UI.AttributeCollection' and 'System.ComponentModel.AttributeCollection'

thanks
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 41810965
Did you use my suggested code?

        System.Web.UI.AttributeCollection attributColl = (System.Web.UI.AttributeCollection)moveAllBtn.Attributes;
        if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
        {
            string myCustomParameterValue = attributColl["CustomParameter"];
        }

Open in new window


It does not look like you are.
1
 

Author Comment

by:mcrmg
ID: 41810992
I am sorry if I confused everyone....this is the latest code I use, and it looks like it is moving..thanks

The error I am getting is
CS0103: The name 'myCustomParameterValue' does not exist in the current context

        foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
        {
            string iText = item.Text;
        }

        System.Web.UI.AttributeCollection attributColl = (System.Web.UI.AttributeCollection)MoveAllbtn.Attributes;
        if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
        {
            string myCustomParameterValue = attributColl["CustomParameter"];
        }

        testBox.Text = myCustomParameterValue;

Open in new window

0
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 250 total points
ID: 41811020
System.Web.UI.AttributeCollection attributColl = (System.Web.UI.AttributeCollection)MoveAllbtn.Attributes;
        if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
        {
            string myCustomParameterValue = attributColl["CustomParameter"];
  testBox.Text = myCustomParameterValue;
}

Open in new window

0
 

Author Closing Comment

by:mcrmg
ID: 41811032
Thank you very much.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question