using NameValueCollection how can I just get textbox contrl values? - asp.net

How do I collect the values of JUST the text box controls?
Is there a way to specify this in ASP.NET 4.0?
ArrayList alAnswerList = new ArrayList();
int iCount = 0;
NameValueCollection frmCollect = Request.Form;
iCount = frmCollect.Count;
for (int i = 0; i <= iCount; i++)
{
alAnswerList.Add(frmCollect.GetValues(i));
}
I am trying to avoid hardcoding an exact index value to start getting values at.
Thanks!
Update: Control Code
<div id="Layer1" runat="server" style="overflow-x: hidden; overflow-y: hidden; padding: 0px 50px 0 50px">
<div id="buttonWrapper">
<asp:Label runat="server" ID="lblStartHunting">text</asp:Label>
<asp:Button runat="server" ID="btnStart" Text="Start" OnClick="btnStart_Click" BorderColor="WhiteSmoke" />
<br />
<br />
<br style="clear: both;" />
<asp:Label runat="server" ID="lblName">Name / Team Name to be added:</asp:Label>
<asp:TextBox runat="server" ID="txtName" Width="280px" BorderColor="WhiteSmoke"></asp:TextBox>
</div>
<div id="huntHeaderWrapper"><div id="huntHeaderPoints"><strong>Points</strong></div><div id="huntHeaderQuestion"><strong>Question</strong></div><div id="huntHeaderAnswer"><strong>Answer</strong></div></div>
<asp:DataList ID="dlHunt" runat="server" Width="740px" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<div id="huntDivPoints">
<asp:Label ID="pointsLabel" runat="server" Text='<%# Eval("points") %>' />
</div>
<div id="huntDivQuestion">
<asp:Label ID="questionLabel" runat="server" Text='<%# Eval("question") %>' />
</div>
<div id="huntDivAnswer">
<asp:TextBox ClientIDMode="Static" ID="huntAnswerText" runat="server" Width="240px" BorderColor="WhiteSmoke"></asp:TextBox><br />
<span style="color: #395069;">Hint: <asp:Label ID="hintLabel" runat="server" Text='<%# Eval("hint") %>' /></span>
</div>
</ItemTemplate>
<ItemStyle BackColor="White" BorderColor="#5885a3" BorderWidth="1" BorderStyle="Solid" />
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connection_private %>" SelectCommand="SELECT [id], [question], [hint], [answer], [points] FROM [table]"></asp:SqlDataSource>
<asp:Button ID="btnShowAnswers" runat="server" Text="Submit Answers" BorderColor="WhiteSmoke" OnClick="btnShowAnswers_Click" /> <asp:Label runat="server" ID="lblSubmit"> text</asp:Label>
</div>

Inside btnShowAnswers_Click, add the following code:
var answers = new List<string>();
if(dlHunt.Items.Count > 0)
{
foreach(DataListItem item in dlHunt.Items)
{
var textBox = (TextBox)item.FindControl("huntAnswerText");
answers.Add(textBox.Text);
}
}
// At this point you have your list of answers and can handle as you see fit

Try this:
// You might not be able access the controls directly here.
//As it might be on a master page,or Simple Page or a
//Content Page or an User Control. Either you have to use FindControl()
//or NamingContainer
foreach(Control c in Page.Controls)
{
if(c is TextBox)
{
var val = ((TextBox)c).Text; // or any other properties of textbox
}
}

Related

MSSQL asp.net insert to table doesnt work

Let me refraise the question
I have an ASP.Net page with data fields and 2 buttons one to check if a company exsists (this one works).
Button 2 needs to insert the values
[ID]
,[BEDRIJF]
,[FUNCTIE]
,[TITEL]
,[VOORNAAM]
,[VOORLETTERS]
,[TUSSENVOEGSEL]
,[ACHTERNAAM]
,[SEXE]
,[ADRES]
,[PCPLAATS]
,[LAND]
,[EMAIL]
,[BRANCHE]
,[TELEFOON]
,[MOBIEL]
,[DATUM_BRIEF]
,[VEBAN]
,[PAYOFF]
From the textboxes radio buttons and dropdown list on the page.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/jubels_logo_vlakje_magenta.png" style="margin-left: 0px" />
<form id="form1" runat="server">
<div style="margin-left: 80px">
<asp:Label ID="Label1" runat="server" Text="Ingevoerd Door :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Bedrijfsnaam :"></asp:Label>
<asp:TextBox ID="TextBox17" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Branche"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="BRANCHE" DataValueField="BRANCHE">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FUP-DataConnectionString %>" SelectCommand="SELECT [BRANCHE] FROM [Branches]"></asp:SqlDataSource>
<br />
<asp:Label ID="Label4" runat="server" Text="Geslacht"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Value="M">Man</asp:ListItem>
<asp:ListItem Value="V">Vrouw</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="Label5" runat="server" Text="Voorletters "></asp:Label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label6" runat="server" Text="Voornaam "></asp:Label>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label7" runat="server" Text="Tussenvoegsel"></asp:Label>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label8" runat="server" Text="Achternaam "></asp:Label>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label9" runat="server" Text="Functie"></asp:Label>
<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Straat - huisnummer"></asp:Label>
<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox10" runat="server" Width="16px"></asp:TextBox>
<br />
<asp:Label ID="Label11" runat="server" Text="Postcode - plaats"></asp:Label>
<asp:TextBox ID="TextBox12" runat="server" Width="41px"></asp:TextBox>
<asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label12" runat="server" Text="Telefoon"></asp:Label>
<asp:TextBox ID="TextBox14" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label13" runat="server" Text="Mobiel"></asp:Label>
<asp:TextBox ID="TextBox15" runat="server">06</asp:TextBox>
<br />
<asp:Label ID="Label14" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="TextBox16" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label15" runat="server" Text="Brieftext"></asp:Label> <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" style="margin-bottom: 0px">
<asp:ListItem>Normaal</asp:ListItem>
<asp:ListItem>EX-Klanten/Propects</asp:ListItem>
<asp:ListItem>Vormgever</asp:ListItem>
<asp:ListItem>Fotograaf</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="Label17" runat="server" Text="Ondertekening"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList4" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>Fred</asp:ListItem>
<asp:ListItem>Jeroen</asp:ListItem>
<asp:ListItem>Fons</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" Text="Check Database op bestaand record" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="Voeg toe aan Database" OnClick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label16" runat="server" Text=""></asp:Label>
<br />
</div>
</form>
</body>
</html>
But this dosnt work, it doenst insert nor does it change the label 16 text to Data Saved or Data not saved.
Can some one point out why the button is not working.
Some insight SQL and SQL connections arent my normal programming area, in normaly build basic windows app with simple layouts and simpel functionality as requested by coworkers.
the button click funtion in aspx.cs file
protected void Button2_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["FUP-DataConnectionString"].ConnectionString;
SqlConnection conn = null;
try
{
SqlCommand cmd = new SqlCommand("sp_insert", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#BEDRIJF", TextBox17.Text);
cmd.Parameters.AddWithValue("#VOORNAAM", TextBox6.Text);
cmd.Parameters.AddWithValue("#ACHTERNAAM", TextBox8.Text);
con.Open();
int i = cmd.ExecuteNonQuery();
if (i != 1)
{
Label16.Text = ("Data Saved");
Response.AppendHeader("Refresh", "5;url=Default2.aspx");
}
else
{
Label16.Text = ("Data Not Saved");
Response.AppendHeader("Refresh", "5;url=Default2.aspx");
}
}
catch (Exception ex)
{
//log error
//display friendly error to user
}
finally
{
if (conn != null)
{
//cleanup connection i.e close
}
}
}
}
try:
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Data Values (#BEDRIJF,#VOORNAAM)";
cmd.Parameters.AddWithValue("#BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("#VOORNAAM", VOORNAAM);
Do not contain your parameters in inverted commas.
Why I'm getting CS1012: "Too many characters in character literal" and CS0019?
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx
As I see you are using Parameter wise Insert so
string BEDRIJF = TextBox17.Text;
string VOORNAAM = TextBox6.Text; // Scrub user data
string VOORLETTERS = TextBox5.Text;
string Tussenvoegsel = TextBox7.Text;
string connString = ConfigurationManager.ConnectionStrings["FUP-DataConnectionString"].ConnectionString;
SqlConnection conn = null;
try
{
conn = new SqlConnection(connString);
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Data Values ('#BEDRIJF','#VOORNAAM')";
cmd.Parameters.AddWithValue("#BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("#VOORNAAM", VOORNAAM);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected == 1)
{
//Success notification
}
else
{
//Error notification
}
}
}
catch (Exception ex)
{
//log error
//display friendly error to user
}
finally
{
if (conn != null)
{
//cleanup connection i.e close
}
See this part where you are wrong
cmd.CommandText = "INSERT INTO Data Values ('#BEDRIJF','#VOORNAAM')";
cmd.Parameters.AddWithValue("#BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("#VOORNAAM", VOORNAAM);

How can i add CSS styling to a asp.net listview?

I know that it's not possible to directly add css styles to a listview but is there any way round this? I'm trying to create a news page where each news block is contained within a box essentially.
Here is my listview code
<div>
<asp:ListView ID="lvNews" runat="server" DataSourceID="SqlNewsDataSource">
<ItemTemplate>
<asp:Label ID="TitleLabel" CssClass="subtitle" runat="server" Text='<%# Eval("Title") %>' />
<br />
<asp:Image ID="news_image" Width="300px" Height="250px" runat="server" ImageUrl='<%# Eval("Imageurl") %>'/>
<br/>
<br />
<asp:Label ID="NewsLabel" CssClass="bodyText" runat="server" Text='<%# Eval("News") %>' />
<br />
<br/>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlNewsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:sqlMUConnectionString %>" SelectCommand="SELECT [Title], [News], [Imageurl] FROM [News]"></asp:SqlDataSource>
</div>
And the css style that Im trying to apply
/*News Page*/
.newsBox {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
margin: auto;
text-align: center;
}
Just wrap the items in your ItemTemplate with a div of that class.
Like this:
<asp:ListView ID="lvNews" runat="server" DataSourceID="SqlNewsDataSource">
<ItemTemplate>
<div class="newsBox">
<asp:Label ID="TitleLabel" CssClass="subtitle" runat="server" Text='<%# Eval("Title") %>' />
<br />
<asp:Image ID="news_image" Width="300px" Height="250px" runat="server" ImageUrl='<%# Eval("Imageurl") %>'/>
<br/>
<br />
<asp:Label ID="NewsLabel" CssClass="bodyText" runat="server" Text='<%# Eval("News") %>' />
<br />
<br/>
</div>
</ItemTemplate>
</asp:ListView>

How to maintain DataList item index across Postbacks?

I'm trying to retain the index of the Datalist item on page reload. I'm trying to do this by storing the index into a Session and then setting the index of DataList object equals to the Session. Here's the HTML code:
<asp:DataList ID="MyList" Width="100%" RepeatDirection="Vertical" runat="server"
OnItemDataBound="MyList_ItemDataBound" OnItemCommand="MyList_ItemCommand">
<ItemTemplate>
<table class="table">
<tr>
<td style="width: 5%;">
<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<asp:Label ID="lbl_isread" runat="server" Text='<%# Eval("isread") %>' Visible="false" />
<input type="checkbox" id="chk_vd" runat="server" class="chkitem" />
</td>
<td style="width: 15%;">
<asp:HyperLink ID="usr" runat="server" Text='<%# Eval("from") %>' />
</td>
<td style="width: 62%;">
<asp:LinkButton ID="sub" Text='<%# Eval("subject") %>' CommandName="select" runat="server"></asp:LinkButton>
</td>
<td style="width: 18%;">
<asp:Label ID="dt" runat="server" Text='<%# Eval("Date_Added") %>' />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table class="table">
<tr>
<td style="width: 5%;">
<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<asp:Label ID="lbl_isread" runat="server" Text='<%# Eval("isread") %>' Visible="false" />
<asp:Label ID="lbl_messagetype" runat="server" Text='<%# Eval("messagetype") %>'
Visible="false" />
<asp:Label ID="lbl_groupid" runat="server" Text='<%# Eval("groupid") %>' Visible="false" />
<asp:Label ID="lbl_content_id" runat="server" Text='<%# Eval("content_id") %>' Visible="false" />
<asp:Label ID="lbl_sendertype" runat="server" Text='<%# Eval("sendertype") %>' Visible="false" />
<input type="checkbox" id="chk_vd" runat="server" class="chkitem" />
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 15%;">
<uc1:avator ID="avt" runat="server" UserName='<%# Eval("from") %>' PhotoName="" Width="65"
Height="65" />
<br />
<asp:HyperLink ID="usr" runat="server" Text='<%# Eval("from") %>' />
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 62%;">
<div class="item_pad_4">
<h3 id="s_sub" runat="server">
<%# Eval("subject") %>
</h3>
</div>
<asp:Label ID="msg" runat="server" Text='<%# Eval("body") %>'></asp:Label>
<div class="bx_br_tp item_pad_4">
<div class="btn-group" id="<%# Eval("id") %>">
<asp:LinkButton ID="a" CssClass="btn btn-primary btn-xs" runat="server" />
<asp:LinkButton ID="r" CssClass="btn btn-primary btn-xs" runat="server" />
<%-- <asp:LinkButton ID="lnk_spam" CssClass="btn btn-primary btn-xs" runat="server"
Text="spam" CommandName="spam"></asp:LinkButton>--%>
</div>
</div>
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 18%;">
<asp:Label ID="dt" runat="server" Text='<%# Eval("Date_Added") %>' />
</td>
</tr>
</table>
</SelectedItemTemplate>
</asp:DataList>
On the server side, I'm trying to restore the index of the DataList item as follows:
if (!Page.IsPostBack)
{
if (Session["datalist"] != null)
{
((DataList)Session["datalist"]).SelectedIndex = (int)Session["index"];
}
Here's the function which does data binding :
protected void MyList_ItemCommand(object source, DataListCommandEventArgs e)
{
Session["datalist"] = source;
string cmd = ((LinkButton)e.CommandSource).CommandName;
long messageid = long.Parse(((Label)e.Item.FindControl("lbl_id")).Text);
string username = ((HyperLink)e.Item.FindControl("usr")).Text;
// approve / ignore action related values
long content_id = 0;
long groupid = 0;
int messagetype = 0;
int index = e.Item.ItemIndex;
Session["index"] = index;
index = (int)Session["index"];
switch (cmd)
{
case "select":
((DataList)source).SelectedIndex = e.Item.ItemIndex;
int isread = int.Parse(((Label)e.Item.FindControl("lbl_isread")).Text);
// mark message as read it its unread message
if (isread == 0)
{
isread = 1; // message is read
MailBoxBLL.Update_isRead(messageid, isread, Page.User.Identity.Name);
}
string _cache = "usr_msg_cnt_" + UserName;
if (HttpContext.Current.Cache[_cache] != null)
{
Cache.Remove(_cache);
}
break;
This doesn't work and Datalist item does not get selected when the page reloads. How can I make it work?
Any help is appreciated.
Add this directly in Page Load:-
protected void Page_Load(object sender, EventArgs e)
{
if (Session["datalist"] != null)
((DataList)Session["datalist"]).SelectedIndex = (int)Session["index"];
}
The probelm with your code is that the Page.IsPostBack property will be false in the initial get request and for every page reload (actual postback) it will be true. Thus !Page.IsPostBack will become actually false and your code will never execute.

ImageButton Visibility not getting changed in aspx.cs page

I'm using a gridview to display some data. There is an image button to display a flag. Within the GridView RowDataBound Event i'm changing the visibility of that button. But the visibility didn't get changed. Here is the code:
bool status = true; // getting this via dataset
ImageButton imgTest = (ImageButton)e.Row.FindControl("ImageButton1");
imgTest.Visible = status;
But still visibility is false.
EDIT:
Here is my HTML Code;
<asp:GridView runat="server" ID="gvScheduleView" DataKeyNames="ControlID,IsPrinted, PrintDate, DueDate"
Width="100%" ShowHeader="true" ShowFooter="true" AutoGenerateColumns="false"
OnRowDataBound="gvScheduleView_rowDataBound" Height="100%" CssClass="removePaddingRight">
<Columns>
<asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("Type") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="120">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Actions" HeaderStyle-HorizontalAlign="Center"
ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div style="text-align: center;">
<table style="margin-left: 28px;">
<tr style="text-align: center;">
<td style="width: 20px">
<asp:ImageButton ID="ibDelete" runat="server" ImageUrl="~/common/images/delete.gif"
OnClick="ibDelete_click" OnClientClick="return confirm('Are you sure you wish to delete this schedule?');"
CommandArgument='<%# Eval("ControlID") %>' ToolTip="Delete Schedule" />
</td>
<td style="width: 20px">
<asp:ImageButton ID="ibRollback" runat="server" ImageUrl="~/common/images/icons/arrow_undo.png"
OnClick="ibRollback_click" CommandArgument='<%# Eval("ControlID") %>' ToolTip="Rollback print generation"
OnClientClick="return confirm('Are you sure you wish to rollback this schedule?');" />
</td>
<td style="width: 20px">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/common/images/icons/email_go.png"
OnClick="ibGenerate_click" CommandArgument='<%# Eval("ControlID") %>' />
<asp:Image ImageUrl="~/common/images/icons/cancel.png" ID="imInfo" runat="server"
Visible="false" ToolTip="DUE DATE IS INVALID - Please change to a future date" />
</td>
<td style="width: 20px">
<asp:ImageButton ID="ibEditProps" runat="server" ImageUrl="~/common/images/icons/building_edit.png"
OnClick="ibEditProps_click" CommandArgument='<%# Eval("ControlID") %>' CommandName='<%# Eval("Type") %>'
ToolTip="Edit Properties" />
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="DivBtnImgCopy" runat="server" visible = "<% =ShowHideButton() %>">
<asp:ImageButton ID="ibEditProps" runat="server"ImageUrl="~/common/images/icons/building_edit.png"
OnClick="ibEditProps_click"CommandArgument='<%# Eval("ControlID") %>' CommandName='<%# Eval("Type") %>' ToolTip="Edit Properties" />
</div>
Code Behind
protected bool ShowHideButton()
{
bool bStatus = false;
try
{
if (sCondition == "false")
{
bStatus = false;
}
else if (sCondition == "true")
{
bStatus = true;
}
return bStatus;
}
catch { }
}

how to find the control OfferID?

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="OfferID" GroupItemCount="2" >
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server" border="0" style="">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td runat="server" style="">
<div id="wrapper">
<div id="ResImage">
<div id="slideshow">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval ("Image1") %>' Width="250px" Height="190px" CssClass="active" />
<asp:Image ID="Image5" runat="server" ImageUrl='<%# Eval ("Image2") %>' Width="250px" Height="190px" />
<asp:Image ID="Image4" runat="server" ImageUrl='<%# Eval ("Image3") %>' Width="250px" Height="190px" />
</div>
</div>
<div id="ResDesc">
<asp:Label ID="lblDesc" runat="server" Width="290px" Height="190px" BackColor="White" Text='<%# Eval("Offer") %>'></asp:Label>
</div>
<div id="ResPrice1">
<asp:Label ID="lblValue" runat="server" Text="Value" CssClass="ResValue"></asp:Label>
<asp:Label ID="lblDiscount" runat="server" Text="Discount" CssClass="ResDiscount"></asp:Label>
<asp:Label ID="lblYouPay" runat="server" Text="You Pay" CssClass="ResYouPay"></asp:Label>
<div id="ResPrice2">
<asp:Label ID="lblValueAmt" runat="server" Text='<%# Eval("Value") %>' CssClass="ResValueAmt"></asp:Label>
<asp:Label ID="lblDiscountAmt" runat="server" Text='<%# Eval("Discount") %>' CssClass="ResDiscountAmt"></asp:Label>
<asp:Label ID="lblYouPayAmt" runat="server" Text='<%# Eval("YouPay") %>' CssClass="ResYouPayAmt"></asp:Label>
</div>
<asp:Label ID="lblRestaurantName" runat="server" Text='<%# Eval("RestaurantName") %>'></asp:Label><br />
<asp:LinkButton ID="lnkGetCoupon" runat="server">Get Discount Coupon</asp:LinkButton>
</div>
<div id="HowItWorks">
<asp:Label ID="lblHowItWorks" runat="server" Text="How It Works?" Font-Bold="True" Font-Size="Small" ForeColor="Red"></asp:Label>
<ul>
<li><asp:Label ID="Label3" runat="server" Text="1.Click on the 'Get Discount Coupon' button" Font-Size="10px"></asp:Label></li>
<li><asp:Label ID="Label4" runat="server" Text="2.Get a print of your Voucher and carry it during your visit to the outlet." Font-Size="10px"></asp:Label></li>
<li><asp:Label ID="Label5" runat="server" Text="3.Show your Voucher and pay the amount directly to the merchant. " Font-Size="10px"></asp:Label></li>
</ul>
</div>
<asp:Label ID="OfferID" runat="server" Text='<%# Eval("OfferID") %>' Visible="false"></asp:Label>
</div>
</td>
</ItemTemplate>
How to find the label control with the id=OfferID...how to use findcontrol here??
i want to find the OfferID of the row on which i click...i have a linkbutton lnkGetCoupon..when i click on the link button...i want to pass the OfferID in the query string to the next page.
i am a new user so they do not let me post answer to my own question
heres the answer...
i added CommandArgument='<%# Eval("OfferID") %> to the link button.
<asp:LinkButton ID="lnkGetCoupon" CommandArgument='<%# Eval("OfferID") %>' runat="server">Get Discount Coupon</asp:LinkButton>
and used the ListView1_ItemCommand
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
Dim offer As String
offer = e.CommandArgument.ToString()
Dim url As String = "~/RestaurantDedicatedPage.aspx?offerID=" + offer
Response.Redirect(url, True)
End Sub
You don't need the Label at all, you can get the OfferID from the DataKeys collection.
First, add a CommandName to your LinkButton:
<asp:LinkButton ID="lnkGetCoupon" runat="server" CommandName="GetCoupon">Get Discount Coupon</asp:LinkButton>
Then use it in the ItemCommand handler:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "GetCoupon")
{
ListViewDataItem item = (ListViewDataItem)e.Item;
int index = item.DataItemIndex;
string offerID = ((ListView)sender).DataKeys[index]["OfferID"].ToString();
Response.Redirect("yourUrl" + offerID);
}
}
Attach to the ListView.ItemCommand event and within that event you can search on the Item in the ListViewCommandEventArgs to find the control you need to alter.
Update your ListView in your ASPX to hook up the ItemCommand event:
<asp:ListView ... OnItemDataBound="ListView1_ItemCommand">
<ItemTemplate>
...
<asp:LinkButton id="lnkGetCoupon" CommandName="View" CommandArgument="<%# Eval("OfferID") %>" />
...
</ItemTemplate>
</asp:ListView>
The ItemCommand event will be fired when a Button or LinkButton (or some other button-esque control) is clicked. To handle this event, add the following
code to your *.aspx.cs (code-behind) file:
protected void ListView1_ItemDataBound(object sender, ListViewCommandEventArgs e)
{
//Check if the lnkGetCoupon button was clicked.
if (string.Equals("View", e.CommandName))
{
//Get the offerID from the CommandArgument.
int offerID = int.Parse(e.CommandArgument);
//Perform your logic using the offerID
}
}

Resources