ASP.Net C# asp:TextBox Only 'Default Value Passed - c#

<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
<ItemTemplate>
<p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox></p>
<div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument='<%#Eval("cartID") %>' Text="Buy"></asp:LinkButton></div>
</ItemTemplate>
</asp:Repeater>
Why does the TextBox cartQty only return the default value of 0 rather than the value entered and submitted? If I change the value to 3 it submits 3 regardless of what's typed.
Here's the codebehind for cartQty
LinkButton lb = (LinkButton)e.CommandSource;
int varCartQty = Convert.ToInt32(((TextBox)lb.Parent.FindControl("cartQty")).Text);
Thank you ;-)

I can only guess:
You are binding the Repeater to it's DataSource on every postback but not only if(!Page.IsPostBack)

I doubt your repeater is rebinded. When you click the button your page_load event is called before your click handler, where your repeater is binded.
So you need to take care of that.
if(!IsPostBack)
{
//Put repeater binding code here
}

Related

Linkbutton id inside a repeater

I have a linkbutton inside a repeater, and I need it to have an id in order to trigger a modal popup extender, but it is not working since the id is not correct due to the repeater.
<asp:Repeater ID="repeaterSessions" runat="server" OnItemCommand="repeaterSessions_OnItemCommand">
<ItemTemplate>
<p>
<%# ((Academia.SessionEN)Container.DataItem).Title %>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Select" CommandArgument=<%#Eval("id") %>></asp:LinkButton>
</p>
</ItemTemplate>
</asp:Repeater>
After this we have the modal popup extender which triggers the linkbutton1:
<ajaxToolkit:ModalPopupExtender ID="LinkButton1_ModalPopupExtender" runat="server" Enabled="True" TargetControlID="LinkButton1" PopupControlID="Panel1">
I tried to add a new linkbutton1 outside the repeater with the id linkbutton1, and it works, but I'd like to know if it is possible to trigger this from inside the repeater.
Thanks everyone!
EDIT: It would also be a possibility to trigger the modal popup extender from the code behind function, but I don't know how to do it either.

Cant change text/value of textbox inside repeater

Hello and thanks for taking your time to help me.
I'm trying to change the text of a textbox thats located inside my repeater.
<asp:Repeater runat="server" ID="rpCategories">
<HeaderTemplate>
<ul id="nav_down" class="nav_down">
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Title") %></li>
</ItemTemplate>
<FooterTemplate>
<li></li>
<li>Contact</li>
<li><a id="cart_logo"></a>
<asp:Panel runat="server" ID="pnlBasket">
<asp:textbox runat="server" id="txtTotalCount" Enabled="false" CssClass="ltTotalCount"></asp:textbox>
</asp:Panel>
</li>
</ul>
</FooterTemplate>
</asp:Repeater>
It's the asp:textbox with the id="txtTotalCount" that I want to change the text of.
Here is my C# code:
TextBox ltTotalCount = (TextBox)FindControl("lblTotalCount");
ltTotalCount.Text = "1";
But if I run the code I get this error : Object reference not set to an instance of an object.
Would be so happy if someone could tell me what I'm doing wrong.
Becuase lblTotalCount is inside a parent control - the repeater, you have to reference it through the repeater.
You should be able to just add the id of your repeater before FindControl, like this...
TextBox ltTotalCount = (TextBox)rpCategories.FindControl("lblTotalCount");
You have to specify the repeater as the parent control to look for the text box and also since it's repeater its quite possible to have more than one text box with that Id so you have to specify which repeater item to look into like so:
TextBox ltTotalCount = rpCategories.Items[0].FindControl("txtTotalCount") as TextBox;
This will return the textbox in the first row of the repeater.
And you should use the Id value not the CssClass value
The ID is probably being 'enhanced' to prevent duplicate IDs in the rendered HTML. You can verify this by looking at the html.
You can add this to the textbox: ClientIDMode="Static" to make the ID stay the same.
You are getting the error because FindControl is returning a null but you are trying to access it anyway.
[Edit] Someone pointed out that this shouldn't work. I agree, here is what does work:
Control FooterTemplate = rpCategories.Controls[rpCategories.Controls.Count - 1].Controls[0];
TextBox MyTextBox = FooterTemplate.FindControl("txtTotalCount") as TextBox;

Catch value in RowDataBound textbox in a gridview with autopostback

I have a gridview that has the columns as below. These columns have textboxes with autopostback = true and need to get their value in RowDataBound of gridview. The problem is that when you type something in the textbox and take the focus off of it, occurs in the RowDataBound grid but the value entered is NOT captured (= /)
Help me to solve this problem, there is more to do. I can not use jquery or anything, only the TextChanged some textbox gridview to get the value.
Code:
<Columns>
<asp:TemplateField HeaderText="Entrada">
<ItemTemplate>
<asp:TextBox ID="txtEmanha_g" AutoPostback="true" class="Mask"
Width="40px" runat="server"
Text='<%#Eval("ENTRADA") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You need to set your GridView up to be editable and avail of the EditItemTemplate. Here's a working example:
Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// Bind grid here only on page load not every post back to the server
}
}

How to use linkbutton in repeater using C# with ASP.NET 4.5

In asp.net I have a table in database containing questions,date of submission and answers.On page load only questions appear.now i want to use any link which on clicking show answers and date specified in table data, either in textbox or label and clicking again on that link answer disappear again like expand and shrink on click.
So what coding should i use for this in C#?
I believe you could handle the ItemCommand event of the Repeater.
Place a LinkButton control for the link that you want the user to click in the Repeater's item template. Set the CommandName property of this to something meaningful, like "ShowAnswers". Also, add a Label or TextBox control into the Repeater's item template, but set their Visible property to false within the aspx markup.
In the code-behind, within the ItemCommand event handler check if the value of e.CommandName equals your command ("ShowAnswers"). If so, then find the Label or TextBox controls for the answers and date within that Repeater item (accessed via e.Item). When you find them, set their Visible property to true.
Note: you could take a different approach using AJAX to provide a more seamless experience for the user, but this way is probably simpler to implement initially.
I think the implementation would look something like this. Disclaimer: I haven't tested this code.
Code-Behind:
void Repeater_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ShowAnswers")
{
Control control;
control = e.Item.FindControl("Answers");
if (control != null)
control.Visible = true;
control = e.Item.FindControl("Date");
if (control != null)
control.Visible = true;
}
}
ASPX Markup:
<asp:Repeater id="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand">
<ItemTemplate>
<asp:LinkButton id="ShowAnswers" runat="server" CommandName="ShowAnswers" />
<asp:Label id="Answers" runat="server" Text='<%# Eval("Answers") %>' Visible="false" />
<asp:Label id="Date" runat="server" Text='<%# Eval("Date") %>' Visible="false" />
</ItemTemplate>
</asp:Repeater>

Dynamic Button in Repeater Postback

I have a nested Repeater control in the ItemTemplate of another Repeater. I want to dynamically add a delete and update button to certain items in the nested Repeater depending on their data value, in this case whether they're associated with the current user. A click to either button should handled server-side based so the data source can be updated, the nested repeater re-bound and an Update Panel that contains both repeaters would be updated.
<asp:Repeater ID="rpProCom" runat="server">
<HeaderTemplate><div class="comment"></HeaderTemplate>
<ItemTemplate>
<div class="c-score"><%# Eval("Text") %></div>
<asp:UpdatePanel ID="upOut" runat="server" ChildrenAsTriggers="true" UpdateMode="Always" >
<ContentTemplate>
<asp:Repeater ID="rpVotes" runat="server" DataSource='<%# ((ScoreBoard.Score)Container.DataItem).Votes %>' OnItemDataBound="rpVotes_ItemDataBound">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li>
<div class="c-ctrl"><asp:PlaceHolder ID="phD" runat="server" /></div>
<div class="c-box"><!-- placeholder for more tools --></div>
<div class="c-i"><div runat="server" ID="imgCom" class='<%# Eval("ImgClass") %>' /></div>
<div class="c-head">
<strong><%# Eval("UserName") %></strong>
<br /><%# Eval("Dt")%>
</div>
<div class="c-body"><%# Eval("Comment") %></div>
</li></ItemTemplate><FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</ContentTemplate></asp:UpdatePanel>
</ItemTemplate>
<FooterTemplate></div></FooterTemplate>
</asp:Repeater>
The outer repeater is bound in Page_Init
protected void rpVotes_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
if (((Vote)e.Item.DataItem).UserName == user.UserName){
LinkButton btn = new LinkButton();
btn.Text = "delete";
btn.CommandName = "delCom";
btn.CommandArgument = ((Vote)e.Item.DataItem).ID.ToString();
btn.Click += new EventHandler(DeleteComment);
btn.ID = "d" + ((Vote)e.Item.DataItem).ID.ToString();
//find contrl
e.Item.FindControl("phD").Controls.Add(btn);
}
}
}
Since the Button is created on item databound and is nested within another control, I can't think of a reasonable way to persist the button and the unique identifier of the Data Item in Viewstate so I can recreate the button(s) on PageInit postback in order to fire the button's Click event.
I tried using Command Arguments, but they are empty:
javascript:__doPostBack('ctl00$CPHRight$rpProCom$ctl02$rpVotes$ctl01$d21','')
I need a reference to the containing Repeater and a unique identifier for the item in the repeater so I can update the DB and rebind that Repeater (ideally in an UpdatePanel that encompasses just that Repeater). I know this is a classic problem, and I'm sure someone's solved it.
So basically, is there some way to force a command argument into a dynamically control created dynamically during ItemDataBound or otherwise identify which dynamically created button caused postback? Can I put and Update Panel in a Repeater? Also can I possibly use the word dynamically again in this post?
You can use dynamically as much as you want :-)
What I would recommend is declaring a static button, adding an itemdatabound event, and showing/hiding the button instead by setting the Visible property of it; that way, you won't have to concern yourself with the reloading issues, but the user won't be able to see the buttons they don't have permissions for.
HTH.

Categories

Resources