Value retrieval problem in listview [Resolved]

Posted by Aaryan under ASP.NET on 3/16/2012 | Points: 10 | Views : 879 | Status : [Member] | Replies : 6
<div>
<b>
<%# Eval("Name") %></b></div>
<div>
$<%# Eval("id") %></div>
<asp:button ID="Button1" text="Buy" runat="server" OnClick ="Button1_Click" />
</asp:button>

In above coding am binding the nameand id in list view.. am adding a button called buy.. if i click i wanna retrieve image name and id

protected void Button1_Click(object sender, EventArgs e)
{
if (ListView1.Items.Count > 0)
{
MessageBox.Show("You click product " +);
}
}

What should be given in mesage box to show the selected image's name and id..




Responses

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Ok then Rewrite your code as:

<div>

<b>
<%# Eval("Name") %></b></div>
<div>
$<%# Eval("id") %></div>
<asp:button ID="Button1" text="Buy" runat="server" commandname='<%# Eval("Name") %>' commandArgument='<%# Eval("Id") %>' OnClick ="Button1_Click" />
</asp:button>


and change in code behind also.

protected void Button1_Click(object sender, EventArgs e)
{
if (ListView1.Items.Count > 0)
{
string name = ((Button)sender).CommandName;
string id = ((Button)sender).CommandArgument;
MessageBox.Show("You click product " +);
}
}



Mark this as answer, if it helps you.................



Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
Thanks for ur reply

'Button' is an ambiguous reference between 'System.Web.UI.WebControls.Button' and 'System.Windows.Forms.Button"

Am getting the error while using the following code in determining the name button
string name = ((Button)sender).CommandName;

string id = ((Button)sender).CommandArgument;


How to overcome it

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Then use this:

string name = ((System.Web.UI.WebControls.Button)sender).CommandName;

string id = ((System.Web.UI.WebControls.Button)sender).CommandArgument;


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Awesome.. nw it is working.. Thanks for ur reply it heplmed me a lot...

Am having one doubt.. what is commandname,commandArgument used for.. why can't we use same name for name and id..
If i wanna retireve three or four values then hw can i retrieve..


Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi CommandName and CommandArgument are properties for button control.
it uses at a RowCommand event of GridView

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aaryan on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
if am using product name,id and price.. how to give with command name and argument

Aaryan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response