get value from textbox within repeater asp.net c# - c#

I've been trying to get this working for a couple of hours now but nothing from google could help me fix the problem.
I have a very simple repeater control:
<asp:Panel ID="userDefDiv" Visible="false" runat="server">
<asp:Repeater ID="userDefRepeater" EnableViewstate="false" runat="server">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="false"></asp:TextBox><br/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
the userDefDiv panel is inside another panel, which is inside contentPLaceHolder.
the parent panel to userDefDiv does NOT have the "enableviewstate="false"".
So.
Everything on this page happens after a couple of linkbuttons_click. so nothing happens during page_load. And after i click another linkbutton i want to get the data from the different textboxes that is within the repeater.
C# code:
This is the code to create all the repeater items.
public void createUserDef()
{
DataTable userDefData;
userDefData = ..... (data from Database.)
userDefDiv.Visible = true;
userDefRepeater.DataSource = userDefData;
userDefRepeater.DataBind();
}
The code for the linkbutton:
protected void linkButton_Click(object sender, EventArgs e)
{
createUserDef();
Label2.Visible = true;
foreach (RepeaterItem item in userDefRepeater.Items)
{
TextBox box = (TextBox)item.FindControl("TextBox1");
string b = box.Text;
Label2.Text += b + " . ";
}
}
As you see i create the repeater once again during the click. But the only thing i can read in label2. is a a number of " .", on dot for each textbox.
but the text from the textbox is empty..
What am I doing wrong??
thanks for reading!
Mattias
SOLUTION:
add EnableVIewState="true" to textbox & repeater.
Dont call call dataBind() before you get the values.
Thanks!

You need to set EnableViewState to 'true' for linkbuttons to work properly in a repeater

Related

CheckBox not Working from inside a Repeater in ASP.NET Webforms

This is what I wrote inside the Repeater
<td style="text-align: center; width: 60px">
<asp:CheckBox ID="DeleteCheckBox" runat="server" CommandArgument='<%# Eval("Id")%>' AutoPostBack="True" OnCheckedChanged="DeleteCheckBox_Click" />
td>
and this is the C#
protected void DeleteLinkbtn_Click(object sender, EventArgs e)
{
try
{
LinkButton BtnId = (LinkButton)(sender);
string ButtonId = BtnId.CommandArgument;
long Active = 1;
if (ButtonId !=string.Empty)
{
if (DeleteCheckBox.Checked==true)
{
objdalTransactionEntry.RentSettingsIsActiveUpdate(Convert.ToInt64(ButtonId), Active);
}
}
}
catch (Exception) { }
}
The Problem is that it is not being able to find the ID of the checkbox DeleteCheckBox in C# code
Based on you code, I have the following comments:
Change you function name, it's confusing. It should either be DeleteCheckBox_Changed or it should not be used for the OnCheckedChanged event
Your sender is not a LinkButton it is the checkbox itself. So you can access the checkbox control with CheckBox checkBoxThatChanged = (CheckBox)(sender)
DeleteCheckBox does not really exist. You have multiple DeleteCheckBoxs due to your repeater, so you won't be able to access it this way.
If you are indeed using a LinkButton, and you want to find a checkbox, then you should add a value to the link button, indicating the row of the table it refers to and iterate through all the checkbox controls until you find your target one.

How to parse data from Gridview row to another page using C# and ASP.NET

I would like to parse a row of data from Gridview in ASP.NET to a second page displaying contents of the row data from the previous page. My Gridview has already been linked to the database.
My current Gridview looks something like this:
I would like to achieve this when I click on the send details hyperlink:
If I click on the second row the data from that row will display in the next page
The following codes are what I had put under my link button itemTemplate:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
<asp:LinkButton ID="viewTours" runat="server" CommandName="view" CommandArgument='<%# Bind("name") %>' PostBackUrl='<%#"~/details.aspx?RowIndex=" + Container.DataItemIndex %>'>View</asp:LinkButton>
</ItemTemplate>
This is my page load method from the second page where I want to load the data from.
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView GridView = (GridView)this.Page.PreviousPage.FindControl("GridView");
GridViewRow row = GridView.Rows[rowIndex];
name.Text = (row.FindControl("name") as Label).Text;
//name.Text = row.Cells[0].Text; (this did not work either, i got the same error)
}
}
However I get a squiggly line on name.Text saying that it does not exist, even though I do have a label with the id name in the design view of my html (second) page.
How can I make it such that I can parse data from the selected Gridview row to another page? Assuming that I can customize the second page and put the labels wherever I like.
This is the code from my gridview. As my binding has been done through the UI I dont have much codes for it, except to redirect the selected gridview row to another page.
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
Session["tour"] = GridView;
Response.Redirect("tourDetails.aspx");
}
I still get the error that the label text does not exist, when it actually exists in the page itself.
Image of the Label with the id=name:
My label DOES contain runat="server":
You are confusing binding data with structure. When you try to get a control with the name key, you really mean Label1, so change
name.Text = (row.FindControl("name") as Label).Text;
to
name.Text = (row.FindControl("Label1") as Label).Text;

getting GridView data from buttonField press?

So I have a GridView for a C# web application, that has a Buttonfield, and when that button is clicked, I need to get the value of one of the fields for that row and store it in a variable for processing in some way.
However, neither the GridView nor the ButtonField seem to possess any means of doing this.
Can anyone recommend a way of getting data from a GridView, or if this is not possible, a different type of view that does offer this functionality, while still displaying a whole table (eg, not a DetailsView)
You can Check this link: https://msdn.microsoft.com/en-us/library/bb907626(v=vs.140).aspx.
Define the CommandName of the Button.
In the GridView Define the RowCommand Event and Check the CommandName.
Get the Index of the Row.
Get the Column with GridView.Rows[index](columnIndex)
If you are using asp:TemplateField like shown below then you can access the row content using RowCommand
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("CustomerID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="AddCode" Text="Add New"/>
Code
protected void gvwSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "AddCode")
{
var clickedButton = e.CommandSource as Button;
var clickedRow = clickedButton.NamingContainer as GridViewRow;
var rows_lblCode = clickedRow.FindControl("lblCode") as Label;
// now you can acccess all the label properties. For example,
var temp = rows_lblCode.Text;
}
}

Panel set to hidden and then visible loses textbox values

I hope I can explain this properly.
I have a grid that is part of our 3rd party shopping cart software. This grid has a row of quantity text boxes into which the customer enters how many of each thing they want to buy.
I put this grid inside a panel so I can set it on or off with
myPanel.Visible=true;
I also have a button to show and one to hide using the above code method.
If I enter a value into a textbox and then click the hide button and then click the show button, when the panel reappears the values are zero. If I then reload the page (browser reload) then the value returns as it was originally. It's a pretty good magic trick but not what I need. What am I doing wrong?
Eventually I want to select a date from a calendar while it is hidden but that is not in play yet... just the show/hide buttons.
Thanks
This sounds like the correct behaviour of ASP.NET WebForms and ViewState
First page load: Panel is visible and the panel loaded with its initial values.
Hide Panel: As soon as the button is pressed a post back occurs. myPanel is set to invisible which on the server side means that the HTML for the panel is not generated (this can be confirmed by looking the generated HTML).
Show Panel: A post back occurs again. But because the values were not rendered in the previous step, they are not available in the ViewState to repopulate the panel.
Reload of the page: This start the process over again (Same as step 1)
A possible solution is to instead hide the panel (<div) on the client-side. This will also have the benefit of not making a round trip to the server to just enable/disable the panel.
Your code should be like below...
Show and Hide button definition to show/Hide the panel is in Java script. That mans there is no handler of Button click event at server side...This approach is suggested normally and fast..
Sample ASPX Code
<script type="text/javascript" language="javascript">
function Hide() {
var ID = document.getElementById('pnl');
ID.style.display = 'none';
return false;
}
function Show() {
var ID = document.getElementById('btnHide');
ID.style.display = 'block';
return false;
}
</script>
<asp:panel id="pnl" runat="server">
<asp:GridView ID="grd" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="ed" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:panel>
<asp:button text="Hide" runat="server" id="Button1" onclientclick="return Hide();" />
<asp:button text="Show" runat="server" id="btnShow" onclientclick="return Show();" />
Sample Code Behind
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (DataTable Dt = new DataTable())
{
using (DataColumn Dc = new DataColumn("Name"))
{
Dt.Columns.Add(Dc);
DataRow dr = Dt.NewRow();
dr["name"] = "1";
Dt.Rows.Add(dr);
dr = Dt.NewRow();
dr["name"] = "2";
Dt.Rows.Add(dr);
grd.DataSource = Dt;
grd.DataBind();
}
}
}
}
}

Parent-Child Gridview in ASP.net

I have two GridView in ASP.net 3.5 page. I have HyperLink field as one of the fields in the First GridView.
On click of this hyperlink I need to call display the 2nd grid by passing some values to a method showAllRecords(value from hyperlink)
How do I do this?
Thanks
You can try a TemplateField like this for GridView1 (primary GridView)
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName" CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and in GridView1's RowCommand, you can get the CommandArgument and setup the DataSource for GridView2 (child GridView).
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName = "cmdName")
{
var arg = e.CommandArgument;
// use arg to filter GridView2's DataSource
GridView2.DataSource = FilteredDataSource;
GridView2.DataBind();
// show GridView2 if it's hidden.
}
}
Maybe the following blog post could give you a hint;
http://www.tugberkugurlu.com/archive/parent-child-view-in-a-single-table-with-gridview-control-on-asp-net-web-forms
First you need to handle SelectedIndexChanged event on the first grid and then get the value from the hyperlink. Is the hyperlink a DataKey? If it is then you get it by GridOne.SelectedDataKey.Values["key"] otherwise get the actual cell by valuefromGridOne = GridOne.SelectedRow.Cells[num].Text where number is the cell number. Once you have it you can pass the value to your second grid by handling Selecting event of the objectDataSource (assuming you use it to bind data) and passing the value like this e.InputParameters["dataKey"] = valuefromGridOne;

Categories

Resources