Please HELP! Need to access textbox control values within a repeater control

D

Darren Smith

Hi There,

I have a shopping cart app that displays products along with a textbox
(to enter quantity) and an image button to add the item to the
shopping cart. Please explain why my below Button_Click procedure
does not retrieve the value of the textbox where the button was
clicked.

Thanks in Advance,

Darren




Sub Button_Click(s as Object, e as ImageClickEventArgs)
Dim Btn As System.Web.UI.WebControls.ImageButton = s
Dim iId as integer
iId=Btn.CommandArgument
Response.write (iId)
Dim tb as System.Web.UI.WebControls.TextBox =
btn.Parent.FindControl("txtValue")
Response.write ("TextBox Value=" & tb.Text)
End Sub
-------------------------------------------------------
<form id="frmProducts" runat="server">
<asp:repeater id="rptProducts" Runat="server">
<ItemTemplate>

a href="ProductInfo.aspx?id=<%# DataBinder.Eval(Container.DataItem,
"prodid") %>"><%# DataBinder.Eval(Container.DataItem, "prodname")
%></a></b></td>

<%# DataBinder.Eval(Container.DataItem, "catdesc") %></td>

<%# formatcurrency(DataBinder.Eval(Container.DataItem,
"retailprice"),2) %>

member price:&nbsp;<b><%#
formatcurrency(DataBinder.Eval(Container.DataItem, "memberprice"),2)
%></b></td>


<asp:TextBox runat="server" width="20" name="txtValue" id="txtValue"
CommandArgument=<%# DataBinder.Eval(Container.DataItem, "prodid") %>
/>


<asp:imagebutton id=btnAddCart CommandArgument=<%#
DataBinder.Eval(Container.DataItem, "prodid") %> BorderWidth="0"
onclick=Button_Click Runat="server" ImageUrl="Frame/btnAdd.gif"/>

</ItemTemplate>
</asp:repeater>
 
D

Darren Clark

Add OnItemCommand="Repeater_Select" to the repeater

then in the code
public void Repeater_Select( object source, RepeaterCommandEventArgs e )

{

}


use the CommandArgument property of the imagebutton to pass thorugh the information you require..

if you need multiple values then do the following
CommandArgument='<%# DataBinder.Eval(Container,"DataItem."+ ITEMONE +","+ ITEMTWO") %>'

then just split on that..
string res = e.CommandArgument.ToString();

string delimStr = ",";

char [] delimiter = delimStr.ToCharArray();

string[] vals = res.Split(delimiter);

string s = vals[0].ToString();

string x = vals[1].ToString();
 
D

dsmith

Hi Darren,

Thanks for your prompt reply, most appreciated.

public void Repeater_Select( object source, RepeaterCommandEventArgs e )

{

}

Does the Repeater_Select Sub remain empty?

commandargument='<%# DataBinder.Eval(Container,"DataItem."+ ITEMONE
+","+ ITEMTWO") %>'

Is the above commandargument added to the imagebutton?

string res = e.CommandArgument.ToString();
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
string[] vals = res.Split(delimiter);
string s = vals[0].ToString();
string x = vals[1].ToString();

Is the above code placed in the Repeater_Select or the ImageButton
On_Click routine?

Thanks,

Darren
 
D

dsmith

Sorry, one more thing...

At which point does the routine pass the value of the quantity textbox?
 
D

Darren Clark

I should have put this in before...
Here is an example..

<asp:Repeater Runat="server" ID="JobPosts" OnItemCommand="Repeater_Select">
<p>
<asp:imagebutton
CommandArgument='<%# DataBinder.Eval(Container,"DataItem."+ JR.Core.Data.JobData.FLD_JOB_ID) %>'
CommandName='EditJob'
id="EditJob" onmouseover="this.src ='../Images/editover.gif'"
onmouseout="this.src ='../Images/editnorm.gif'" Runat="server"
ImageUrl="../Images/editnorm.gif"
AlternateText="Edit Job">
</asp:imagebutton>
</p>
</asp:Repeater>


When the imagebutton is clicked it will push the event up to the repeaters onitemcommand


public void Repeater_Select( object source, RepeaterCommandEventArgs e )
{
///<summary>
/// THis is when a button is selected from inside the repeater.
///</summary>

// Get the JobID and then send the user to the correct page.
string JobID = e.CommandArgument.ToString();

Session["JobID"] = e.CommandArgument.ToString();

if (e.CommandName.ToString() == "JobResults")
{
Response.Redirect("JobResults.aspx");
}
else if(e.CommandName.ToString() == "EditJob")
{
Response.Redirect("EditJob.aspx");
}
else if (e.CommandName.ToString() == "ArchiveJob")
{
// this is used as the CommandArgument passess 2 values through as s delimtered string eg. val1,val2

string res = e.CommandArgument.ToString();
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
string[] vals = res.Split(delimiter);
string s = vals[0].ToString();
string x = vals[1].ToString();
int jobID = Convert.ToInt32(s);
int status = Convert.ToInt32(x);
JobManager man =new JobManager();
man.UpdateJobStatus(jobID,status);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top