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");
}
}
Related
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.
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"];
}
I need to display a modal popup when gridview row is clicked(without any buttons), i managed to display popup by clicking the gridview row but dont know to bind values to textbox displayed in modal popup..
So i need help to
Bind gridview row(when clicked) values to modal popup textboxes
Modal popup textboxes should only be editable when a edit button(placed at bottom of modal popup) is clicked
After editing work is done, it should save the edited data when clicked save button(placed at bottom next to edit button)
As am new to asp.net please try to help me with your suggestions for my requirements.
Because of below code am able to show popup wen clicked gridview row but suggest me how to bind values to textbox:
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.DataItem == null)
{
return;
}
try
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
break;
case DataControlRowType.DataRow:
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(grid, "Select$" + e.Row.RowIndex.ToString()));
e.Row.Attributes.Add("onclick", String.Format("javascript:$find('{0}').show();", ModalPopupExtender2.ClientID));
TextBox1.Text = grid.SelectedRow.Cells[0].Text;
TextBox2.Text = grid.SelectedRow.Cells[1].Text;
TextBox3.Text = grid.SelectedRow.Cells[2].Text;
TextBox4.Text = grid.SelectedRow.Cells[3].Text;
TextBox5.Text = grid.SelectedRow.Cells[4].Text;
ModalPopupExtender2.Show();
break;
}
}
catch
{
return;
}
}
HTML CODE:
<asp:Panel ID="editpanel" runat="server">
<table width="850px" border="1" class="color">
<tr>
<td align="center">
<asp:Label ID="Label1" runat="server" Text="Firstname" Width="150px"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Surname" Width="150px"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Visits" Width="150px"></asp:Label>
<asp:Label ID="Label4" runat="server" Text="$ Speed" Width="150px"></asp:Label>
<asp:Label ID="Label5" runat="server" Text="Points" Width="150px"></asp:Label>
<asp:ImageButton id="ImageButton1" runat="server" src="close.png" onclick="close_Click" style="float:right; height: 16px;" ToolTip="To close window"/>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:ImageButton ID="cancell" runat="server" onclick="cancell_Click"
src="cancel.jpg" style="float:right; width: 16px;"
ToolTip="To cancel member" />
<asp:ImageButton ID="tickk" runat="server" onclick="tickk_Click" src="tick.jpg"
style="float:right; height: 16px;" ToolTip="To save member" />
<asp:ImageButton ID="Edit" runat="server" onclick="edit_Click"
src="edit.jpg" style="float:right; height: 16px; width: 16px;"
ToolTip="To edit member" />
</td>
</tr>
</table>
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="modal2" PopupControlID="editpanel" BackgroundCssClass="modalBackground"></asp:ModalPopupExtender>
<asp:ImageButton ID="modal2" runat="server" src="addmember.jpg" OnClick="modal2_click" Text="modal2" style="display:none;"/> // have created a dummy image button
Since am new to asp.net please try to help me with your suggestions.
Use this to bind your data with text box by using BindingSource (Just an example):
textBoxid.DataBindings.Add(new Binding("Text", customersBindingSource, "ID"));
textBoxname.DataBindings.Add(new Binding("Text", customersBindingSource, "NAME"));
textBoxcity.DataBindings.Add(new Binding("Text", customersBindingSource, "City"));
To make uneditable your text boxes, use this:
private void EditButton_Click(object sender, EventArgs e)
{
if (textBoxid.ReadOnly == true)
{
textBoxid.ReadOnly = false;
//how many text boxes you have, do the same here
}
else
{
textBoxid.ReadOnly = true;
//how many text boxes you have, do the same here
}
}
To save your edits, just run an UPDATE query with all your new data.
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.
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()