Get value from INPUT inside GridView - c#

I have a GridView with a non ASP object (INPUT type for suggestions) and i want to get the values from those forms to the code behind, how do I do it?
<ItemTemplate>
<tr style="background-color: #E0FFFF; color: #333333;">
<td style="text-align: left;padding-left:10px">
<asp:Label ID="DESLabel" runat="server" Text='<%# Eval("DES") %>' />
</td>
<td style="text-align: left">
<input id ="PROD" style="width:100%;Height:25px" />
</td>
<td>
<asp:Label ID="PRODQUANTLabel" runat="server" Text='<%# Eval("PRODQUANT") %>' />
</td>
<td>
<asp:TextBox ID="AVQUANTLabel" runat="server" Height="25px" Width="65px" Text='<%# Eval("AVQUANT") %>' />
</td>
</tr>
</ItemTemplate>

you can user findcontrol meyhod
in for loop or in RowUpdating Event
1.firt way in Loop, 2 RowUpdating Methid
foreach (GridDataItem item in RadGrid1.Items)
{
string id = item["ID"].Text;
string firstName = (item["TempColumn1"].FindControl("PRODQUANTLabel") as Lable).Text;
}
protected void GridView1_RowUpdating(object sender,idViewUpdateEventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow gvRow = gv.Rows[e.RowIndex];
Lable tb = (Lable) gridview1.FindControl("PRODQUANTLabel");
if (tb == null)
throw new ApplicationException("Could not find Lable");
string strValue= tb.Text;
}

You can get the value on code behind as any simple form post. To do that you first make sure that you have a name field on your input control.
<input id ="PROD" name="prod" style="width:100%;Height:25px" />
then on post back you can get the value using the Request.Form
var value = Request.Form["prod"]

I ended up using another method using asp texboxes. Still working on in.

Related

Have Checkbox inside repeater send data from Textbox inside repeater to another Textbox outside of repeater

I have a repeater that populates a list of 3 columns and has a Checkbox next to each row. I am trying to create a scenario in which a person checks a row, the page locates the "Portion Name" text box inside of the repeaters row that corresponds with the row where the checkbox has been clicked, and once that checkbox is selected, it sends the Portion Name to another textbox outside the repeater called "testTextBox.Text". I have my code below, and am sure I am missing something as I have not done a "OnCheckChanged" event before, I am only familiar with onTextChanged events.
Below is the code:
<asp:Repeater ID="rptAccount" runat="server" OnItemCommand="rptAccount_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>Account
</th>
<th>Portion ID
</th>
<th>Portion Name
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="Account" runat="server" Width ="50px" Text='<%#Eval("Account") %>' ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="PortionID" runat="server" Width ="90px" Text='<%#Eval("Portion ID") %>' ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="PortionName" runat="server" Width ="340px" Text='<%#Eval("Portion Name") %>'></asp:TextBox>
</td>
<td>
<asp:CheckBox ID="Name" runat="server" OnCheckedChanged = "TbName_CheckedChanged" Checked='<%# Eval("Name").ToString() == "True" %>' ></asp:CheckBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
cs code:
protected void TbName_CheckedChanged(object sender, EventArgs e)
{
var PortionName = (sender as TextBox).Parent;
var rptAccount = (sender as TextBox).Parent;
var checkedd = rptAccount.FindControl("Name") as CheckBox;
var PortionNamee = rptAccount.FindControl("PortionName") as TextBox;
if (checkedd.Checked)
{
testTextBox.Text = PortionNamee.Text;
}
}
Thank you for any help you can offer.
The repeater ends up having a ton of controls with the name Name. Remember the template is repeated many times. you need to find the index of the current item. An easier way its to attach an attribute to the checkbox to hold the text value, that way you can extract it straight from the sender without having to worry about parent and index. Try this:
<asp:Repeater ID="rptAccount" runat="server" OnItemCommand="rptAccount_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>Account
</th>
<th>Portion ID
</th>
<th>Portion Name
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="Account" runat="server" Width ="50px" Text='<%#Eval("Account") %>' ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="PortionID" runat="server" Width ="90px" Text='<%#Eval("Portion ID") %>' ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="PortionName" runat="server" Width ="340px" Text='<%#Eval("Portion Name") %>'></asp:TextBox>
</td>
<td>
<asp:CheckBox ID="Name" runat="server" OnCheckedChanged = "TbName_CheckedChanged" CommandName='<%#Eval("Portion Name") %>' Checked='<%# Eval("Name").ToString() == "True" %>' ></asp:CheckBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
cs code:
protected void TbName_CheckedChanged(object sender, EventArgs e)
{
var checkedd = sender as Checkbox;
if (checkedd.Checked)
testTextBox.Text = checkedd.Attributes["CommandName"];
}

listview item said to be "Null"

I have a list view which is below.....
I am trying to read the Quantity and Available based on their ProductID
I want to read the data in the listview and then i will view the error if Quantity > Available
Now when ever i do so, the items are Null
here is ASPnet:
<asp:ListView ID="lvCart" runat="server" onitemcommand="lvCart_ItemCommand">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lbproductid" runat="server"
class="form-control" Width="50"
Text='<%# Eval("ProductID") %>' />
</td>
<td>
<%# Eval("Name") %>
<asp:Literal ID="ltRefNo" runat="server"
Text='<%# Eval("RefNo") %>' Visible="false" />
</td>
<td>
Php <%# Eval("Price", "{0: #,###.00}") %>
</td>
<td>
<asp:TextBox ID="txtQty" runat="server"
class="form-control" type="number"
min="1" max="99" MaxLength="2" Width="70"
Text='<%# Eval("Quantity") %>' required />
<asp:Label ID="avail" runat="server"
class="form-control" Width="50"
Text='<%# Eval("Available") %>' />
</td>
Code Behind 1:
(here is what i tried)
void getavail()
{
Label lbproductid = (Label)lvCart.FindControl("lbproductid");
Label avail = (Label)lvCart.FindControl("avail");
//ITemplate ID = (ITemplate)lvCart.FindControl("ProductID");
TextBox txtQty = (TextBox)lvCart.FindControl("txtQty");
if (int.Parse(txtQty.Text) > int.Parse(avail.Text))
{
error.Visible = true;
}
con.Close();
}
Code behind 2:
(Second trial)
foreach (ListViewDataItem dataitem in lvCart.Items)
{
Label lbproductid = (Label)lvCart.FindControl("lbproductid");
Label avail = (Label)lvCart.FindControl("avail");
//ITemplate ID = (ITemplate)lvCart.FindControl("ProductID");
TextBox txtQty = (TextBox)lvCart.FindControl("txtQty");
if (int.Parse(txtQty.Text) > int.Parse(avail.Text))
{
error.Visible = true;
}
con.Close();
}
When trying the second codes, it does not read codes after it. Starting at the line:
Label lbproductid = (Label)lvCart.FindControl("lbproductid");
and then it directs to last bracket and close();
I am putting this GetAvail in the page load event as i want it to show right away.
help this got me hours. thanks in advance

Changing Readonly attribute of Textbox within a repeater

On my aspx page i have a repeater with 5 textboxes with 1 imagebutton to edit the row
these textboxes are readonly, to edit them I need them to not be readOnly..
In my behind code i am using :
protected void EditRecipeInfo(object sender, CommandEventArgs e)
{
ImageButton ib = sender as ImageButton;
TextBox titleTXT = (TextBox)ib.FindControl("titleRepeat");
TextBox qtyTXT = (TextBox)ib.FindControl("qtyRepeat");
TextBox uomTXT = (TextBox)ib.FindControl("uomRepeat");
TextBox prepTXT = (TextBox)ib.FindControl("prepRepeat");
TextBox orTXT = (TextBox)ib.FindControl("orRepeat");
titleTXT.ReadOnly = false;
qtyTXT.ReadOnly = false;
uomTXT.ReadOnly = false;
prepTXT.ReadOnly = false;
orTXT.ReadOnly = false;
////
}
But when I fire this event the break points show me that the property is being set to false, but when I click to delete any value in the textbox it still acts like a readonly
UPDATE:
<asp:Repeater ID="ingredRepeater" runat="server">
<HeaderTemplate>
<table style="width: 100%">
<tr>
<th></th>
<th></th>
<th><h2>Title</h2></th>
<th><h2>Qty.</h2></th>
<th><h2>UoM</h2></th>
<th><h2>Prep.</h2></th>
<th><h2>Alternate</h2></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:ImageButton Style="height: 25px; width: 25px;" ImageUrl="/img/edit.png" Visible="true"
ID="editRecipeInfo" AutoPostBack="true" runat="server" OnCommand="EditRecipeInfo" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>' />
</td>
<td>
<asp:ImageButton ImageUrl="/img/RedX.png" ID="button2" runat="server" Height="20"
Width="20" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>'
OnCommand="deleteRecipeView" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly="true" ID="titleRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'
size="45" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="qtyRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Quantity") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="uomRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UnitsOfMeasure") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="prepRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Prep") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="orRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AlternativeIngredients") %>'
size="20" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Make sure you're not re-binding the repeater after you set the ReadOnly property to true.
I agree with #Tariqulazam , the markup would help.
Assuming your code comes from an ItemCommand event handler, I am quite surprised to see the FindControl applied to the ImageButton.
I guess your code should be something like this :
void rpAcces_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//...
ImageButton ib = sender as ImageButton;
TextBox titleTXT = (TextBox)e.Item.FindControl("titleRepeat");
TextBox qtyTXT = (TextBox)e.Item.FindControl("qtyRepeat");
TextBox uomTXT = (TextBox)e.Item.FindControl("uomRepeat");
TextBox prepTXT = (TextBox)e.Item.FindControl("prepRepeat");
TextBox orTXT = (TextBox)e.Item.FindControl("orRepeat");
titleTXT.ReadOnly = false;
qtyTXT.ReadOnly = false;
uomTXT.ReadOnly = false;
prepTXT.ReadOnly = false;
orTXT.ReadOnly = false;
//...
}
Also, be aware that you can not rebind your repeater later in the page life-cycle without losing these changes.
And watch out for any Enabled attribute set on your TextBoxes
Once again, difficult to answer without the whole code.

How I get the right Line Index of the ListView

I wrote a ASP.NET Application that get Data from the Active Directory. I use a ListView to display this data. The User input a String (Lastname or a part of this) in a TextBox. Than the ListView list all AD Users with the same string from the TextBox. Every Line get a Button "Anzeigen" to get more Informations about the User. This ListView has six columns and every line show a User. in column number six is the button "Anzeigen". If a User click on this button open a new WebForm "benutzer.aspx" with more Informations abaout this seleced User and get a Session Value "email" from the line.
My Problem:
I don't know how I get the Index of the Line of the ListView that I need for the Session Value.
My Code:
cs file:
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Anzeigen")
{
//This give me everyone the Value -1 back
int selectedLine = myListView.SelectedIndex;
//I need the Line Index for the right Value
Label lb = (Label)myListView.Items[selectedLine].FindControl("Label2");
string email = lb.Text;
Session["email"] = email;
Response.Redirect("Benutzer.aspx");
}
}
ASPX File:
...
<ItemTemplate>
<tr runat="server">
<td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="myArgument" runat="server" /></td>
</tr>
</ItemTemplate>
...
I search and I found listview selectedindices but it don't work :( and I dont't can use it in my Application .
tarasov
Use ListView's ItemCommand rather than Button's on command
see http://www.codeproject.com/Articles/24570/Complete-ListView-in-ASP-NET-3-5
for more detail.
One more thing From example you can see that author has extracted the values from e.Item. You can pass the key(email,username or whatever) as CommandArgument and can access that value directly from command argument.
how to pass it
<asp:LinkButton ID="myLink" runat="server" CommandName="Anzeigen" CommandArgument='<%#Eval("KeyColumn")%>'>Anzeigen</asp:LinkButton>
Also use Linkbutton rather than Asp:Button
ASPX:
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' runat="server" /></td>
CS:
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
Label lb = (Label)myListView.Items[index].FindControl("Label2");
string email = lb.Text;
Session["email"] = email;
Response.Redirect("Benutzer.aspx");
}
}

FormView.FindControl(): object reference error

I have a formview that has several textboxes inside of tr/td's. I'm trying to get the textboxes by using the .FindControl method but it's coming back null. The FormView is always in Edit mode (so I'm always in the EditItemTemplate) and i'm trying to load querystring values into the textboxes coming from the previous page so I do need this to happen on page_load. I do this on Gridviews all the time like this:
txtFirstName = (TextBox)fvGeneralInfo.FindControl("txtFirstName");
or like this:
txtFirstName = (TextBox)fvGeneralInfo.FooterRow.FindControl("txtFirstName");
or like this:
txtFirstName = (TextBox)fvGeneralInfo.Rows.FindControl("txtFirstName");
What gives?
<asp:FormView ID="fvGeneralInfo" runat="server"
DataSourceID="objInstructorDetails"
OnItemCommand="fvGeneralInfo_ItemCommand"
OnItemUpdated="fvGeneralInfo_ItemUpdated"
DefaultMode="Edit"
DataKeyNames="InstructorID" >
<EditItemTemplate>
<table>
<tr>
<td colspan="2" class="Admin-SubHeading" style="padding-left:10px;">General Info:</td>
</tr>
<tr>
<td class="Admin-FieldLabel">ID:</td>
<td><asp:TextBox ID="txtInstructorId" runat="server" CssClass="Admin-Textbox" ReadOnly="true" Text='<%# Bind("InstructorID") %>' /></td>
</tr>
<tr>
<td class="Admin-FieldLabel">First Name:</td>
<td><asp:Textbox ID="txtFirstName" runat="server" CssClass="Admin-Textbox" Text='<%# Bind("FirstName") %>' /></td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
abatishchev's answer is right, although I found this variation a bit neater: it avoids having to call DataBind() explicitly.
<asp:FormView ID="fvMember" runat="server" DataSourceID="tblMembers" DefaultMode="Insert" OnDataBound="DataBound">...</asp:FormView>
protected void DataBound(object sender, EventArgs e)
{
if (fvMember.CurrentMode == FormViewMode.Edit)
{
Label lblSubmit = fvMember.FindControl("lblSubmit") as Label;
...
}
}
Call DataBind(); first. Then FindControl()

Categories