mcrmg
asked on
Passing parameter to code behind
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
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
in the front end, I have the code like this
<asp:Button id="MoveAllbtn" onclick="MoveAllbtn_Click"
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;
}
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
ASKER
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.Butt on'
CS0104: 'Button' is an ambiguous reference between 'System.Web.UI.WebControls
This is how you fix that. However, I don't think this code is going to function the way you want.
Can you explain more on what you're trying to do?
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;
}
Can you explain more on what you're trying to do?
Yes, as Daniel said use System.Web.UI.WebControls. Button
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;
}
ASKER
I am getting this error (sorry)
CS0104: 'AttributeCollection' is an ambiguous reference between 'System.Web.UI.AttributeCo llection' and 'System.ComponentModel.Att ributeColl ection'
CS0104: 'AttributeCollection' is an ambiguous reference between 'System.Web.UI.AttributeCo
Use System.Web.UI.AttributeCol lection there
You'd have to cast. Seems strange. You're adding too many using statements.
Get rid of your Windows using if you can.
System.Web.UI.AttributeCollection attributColl = (System.Web.UI.AttributeCollection)moveAllBtn.Attributes;
if (attributColl != null && attributColl.Count > 0 && attributColl["CustomParameter"] != null)
{
string myCustomParameterValue = attributColl["CustomParameter"];
}
Get rid of your Windows using if you can.
ASKER
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;
}
Does it work now?
ASKER
this is the error I am getting
CS0138: A using namespace directive can only be applied to namespaces; 'System.Web.UI.AttributeCo llection' is a type not a namespace
CS0138: A using namespace directive can only be applied to namespaces; 'System.Web.UI.AttributeCo
Remove following line:
using System.Web.UI.AttributeCol lection;
using System.Web.UI.AttributeCol
ASKER
After removing,
CS0104: 'AttributeCollection' is an ambiguous reference between 'System.Web.UI.AttributeCo llection' and 'System.ComponentModel.Att ributeColl ection'
thanks
CS0104: 'AttributeCollection' is an ambiguous reference between 'System.Web.UI.AttributeCo
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much.
Open in new window