I've struggled with this for days now, trying to access html inputs, i.e. checkboxes or textboxex generated at runtime based on DB results. My HTML which is added as a literal control to a asp placeholder looks like this:
<div class='cart-item'>
<div class='product-name'> <h3>gold baseball figure</h3></div>
<img src='Graphics/gold-trophy.png'></img>
<div class='inventory-short-description'> <p>A finely crafted gold plated baseball figuring to top your choice of trophey base (sold seperatly).</p></div>
<div class='clear'></div>
<div class='item-price'><p>$22.95</p></div>
<div class='cart-item-checkbox'><label><input type='checkbox' id='1' name='1' runat='server'/> Select </label>
<a href='products.aspx?viewItem=1'>view item </a></div></div
Question is, how do I access this check box in the .cs code behind page?
My code which is generating this html and adding it to the page is in the overrided OnInit method. Looking at the placeholder on postback shows that the checkbox is in the literal control.
I've tried:
Page.FindControl() returns null when searching for dynamic control
ASP.NET page_init event?
FindControl() return null
http://forums.asp.net/t/1336244.aspx?finding+HTML+control
....And countless others.
Yesterday I used a hackish way to set the value of these checkboxes to a asp.net hidden field using jquery. My coworkers (all java devs) say this seems to be the wrong way to access these elements. Is there a better way? I'm beginning to think I've coded myself into a spot I can't get out of.
Thanks for the help.
In your .aspx
<asp:Repeater ID="rptItems" runat="server" EnableViewState="False">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" />
</ItemTemplate>
</asp:Repeater>
In your .aspx.cs file you can access repeater in postback events as below
foreach (RepeaterItem rptItem in rptItems.Items)
{
TextBox txtName = (TextBox)rptItem.FindControl("txtName");
if (txtName != null) { System.Diagnostics.Debug.Write(txtName.Text); }
}
Related
On MarkUp in my aspx page form I have these two TextBox :
<asp:TextBox ID="Mtl" runat="server" ReadOnly="true" Enabled="false"></asp:TextBox>
<asp:TextBox ID="ps" runat="server"></asp:TextBox>
The HTML view for these two TextBox is :
<input name="Mtl" type="text" value="901" readonly="readonly" id="Mtl" disabled="disabled" />
<input name="ps" type="text" id="ps" />
Now I need insert next to the TextBox with id ps the HyperLink where passed in querystring the value of TextBox with id Mtl, the value is 901.
I need pass this value for working in another aspx page.
I have tried this solution but the HyperLink is not clikable :
<asp:HyperLink ID="HlLink" runat="server"
NavigateUrl='<%# String.Format("~/box.aspx?v={0}&e={1}&l={2}", "y", "IC", HttpUtility.UrlEncode(Eval("Mtl").ToString())) %>'
ImageUrl="~/Images/edit_icon.gif" Target="_blank" Text="Mtl"></asp:HyperLink>
In this aspx page I don't have GridView, maybe it does not work for this reason ?
How to do resolve this ?
Please help me, thank you so much in advance.
Yes you are correct since your control is not inside a gridview (or any databoundcontrol for that matter) that's why it will not work.
Actually, <%# %> is called data bind expressions and they are evaluated for data bound controls only. For your HyperLink control to work with this code nugget you will have to explicitly call the DataBind method on that control like this:-
protected void Page_Load(object sender, EventArgs e)
{
HlLink.DataBind();
}
You can bind a jQuery 'Change' event on your textbox. And in this event you can set correct navigation url by picking up the value from the textbox and appending it in appropriate place in your query string. Its a fairly easy solution. If you don't know how to do it I can provide you a sample.
I'm trying to change the CSS class and attributes of a set of asp.net controls via the code behind using this:
ASP.NET:
<span id="followbtn_mainbtn" runat="server" class="follow-btner" onclick="profile_followers_follow(this)">
<img id="img_followingbtn" runat="server" class="profile-single-profile-follow-btn-img" src="icons/profico/following.png" style="display: none;">
<img id="img_unfollowbtn" runat="server" class="profile-single-profile-unfollow-btn-img" src="icons/profico/unfollow.png" style="display: none;">
<img id="img_followicon" runat="server" class="profile-single-profile-follow-icon" src="icons/profico/followIcon.png">
<span id="span_following_text" runat="server" class="profile-single-profile-follow-btn-following">Follow</span>
<span id="span_unfollow_text" runat="server" class="profile-single-profile-follow-btn-unfollow" style="display: none;">Unfollow</span>
</span>
Code:
followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");
span_unfollow_text.InnerText = "Following";
img_followingbtn.Attributes.CssStyle.Add("display", "block");
img_unfollowbtn.Attributes.CssStyle.Add("display", "block");
span_unfollow_text.Attributes.CssStyle.Add("display", "block");
However when I run this, I do not see the desired results. If I hard code the appropriate classes to the controls, they work properly but the code doesn't do it dynamically.
What am I doing wrong?
Are you updating the css during a postback? First try adding the changes to your Page_Load method on the form and that will tell you that your code is working when setting the styles and classes. If the code works then I would make sure you have EnableViewState="false" on your page and/or parent control of the span.
It was actually a simple error. I've used the wrong code to change the css class of the span named "followbtn_mainbtn".
I replaced this
followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");
with this
followbtn_mainbtn.Attributes["class"] = "follow-btner-no-hoverer";
Voila! Done deal. Thanks for all your answers and comments though :)
You can use this code to change css in code behind
img_followingbtn.Style.Add("display", "block");
My .aspx looks like the following:
<div id="valueIntroduction" type="text" class="labelarea" runat="server">
<input name="$labeluniversityid" type="text" id="labeluniversityid" style="color:#7D110C;border:0;background-color:#C0D5F2;width:100%" />
</div>
.cs File looks like:
if (results.Read())
{
labeluniversityid.value = results["TEXT_CONTENT"].ToString();
}
Exactly what am trying do is am getting the data from the database and displaying it in the valueIntroduction div. That is workiing fine. Now i added a text box with readonly mode. So that in my page if I press EDIT button, the value could be edited.
Use a TexBox component:
<asp:TextBox ID="labeluniversityid" runat="server" CssClass="yourcssclass"></asp:TextBox>
As for the styling:
.yourcssclass
{
color:#7D110C;
border:0;
background-color:#C0D5F2;
width:100%
}
Then, in your code behind you can easily use it like this:
labeluniversityid.Text = results["TEXT_CONTENT"].ToString();
Keep in mind that ASP.NET Controls are translated into common HTML tags, so you can wrap it and style it as you would with any other normal input of type text.
Also: type="text" is not valid for div
Try putting runat="server" attribute in <input id="labeluniversityid"> tag.
Or use a <asp:TextBox> control as areks suggests.
You need to add -
runat="server"
to your input field
Or, even better, use an
<asp:textBox ..>
I'm learning C# and working on my first ASP.Net e-commerce website.
I have the following repeater:
<asp:Repeater ID="Cartridges" runat="server" OnItemCommand="Cartridges_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton>
</ItemTemplate>
However, when the code is rendered the repeater produces JavaScript to postback which is no good when populating a shopping cart if the user has JavaScript turned off!
<div class="wrap-cart">
<div class="cartbuy"><a id="ContentPlaceHolder1_Cartridges_buy_0" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Cartridges$ctl00$buy','')">Buy</a></div>
<div class="cartimg"><img src="pic/epson-comp-set50.jpg" alt="product" /></div>
<div class="carttext">
<p class="cartdesc"><span class="homeprinters">product</span></p>
<p class="cartprice">£x.xx</p>
</div>
All I want to do is to pass the cartID to a data table. Is there a way round it so JavaScript is not involved at all?
For completeness here's my code behind:
protected void Cartridges_ItemCommand(object source, RepeaterCommandEventArgs e)
{
cartidtest.Text = "added";
if (e.CommandName == "AddtoCart")
{
string varCartID = (e.CommandArgument).ToString();
//more code here
}
}
Sure, you can pass the selected ItemID via QueryString
First, Generate an anchor tag like this:
AddToCart
This may be accomplished in an ItemTemplate:
<a href='<%# "NextCheckoutStep.aspx?ItemID=" + Eval("ItemID").ToString() %>'>AddToCart</a>
Finally, in NextCheckoutStep.aspx, access:
Request.QueryString["ItemID"]
Notes
You can pass information back to the same page. Just replace NextCheckoutStep.aspx with CurrentCheckoutStep.aspx. You will just need to put some routing code into your Page Load (or init) handlers (with postback = false) to react to the various page states that are created by this method. Sometimes a state diagram is helpful in working with this design.
Make sure to 100% sanitize your QueryString-Reads, because malicious users will attempt to put nasty values in the query string.
You don't have to use the QueryString, you can also get by with the HTTP POST collection; however, you need to sanitize here too, because you can never trust inputs coming from the client.
I would have a link which includes the item id in a querystring. I would expect the user's cartId to be stored in a cookie or in a session variable.
<asp:Repeater ID="Cartridges" runat="server">
<ItemTemplate>
<p>Buy</p>
</ItemTemplate>
</asp:Repeater>
A LinkButton renders as a link that immediately forces a submit, so it needs javascript.
You will have to use a plain HyperLink to render a url like mypage.aspx?id=cartid.
You can use instead of LinkButton
Say I have a dynamically generated LinkButton in my ascx.cs code-behind. How could I go about "printing" this control to my page? Obviously I can't do something like just print the Text property as I need the button to retain its hyperlink. I'm guessing I want to use the WebControl.Render method, but I'm not familiar with it at all and haven't been able to find a good example of its use.
This article http://www.tomot.de/en-us/article/3/asp.net/create-a-control-in-the-codebehind-and-retrieve-its-rendered-output should explain the basics of what you are looking for.
You could provide a label at the right location in the page: <asp:Label id="myLinkButtonPlace" runat="server"></asp:Label>, and in code, you could add the linkbutton to the controlcollection of the label: this.myLinkButtonPlace.Controls.Add(aLinkButton);
Following on from #Joachim VR, there are many other asp.net controls with which you can add a dynamically created control to.
<asp:Label id="Label1" runat="server" />
<asp:PlaceHolder id="Placeholder1" runat="server" />
<asp:Panel id="Panel1" runat="server" />
The above would render HTML different.
So the Label would render as a <span id="Label1"><a></a></span>
Panel as <div id="Placeholder1"><a></a></div>
The Placeholder would simply render as the <a></a>