how to get value of textbox in listview? - c#

<asp:ListView runat="server" ID="lvAttachments" ItemPlaceholderID="ph" OnItemDataBound="lvAttachments_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnReject" OnClick="btnReject_Click"></asp:LinkButton>
<asp:TextBox runat="server" ID="tbReason" CssClass="textbox" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:ListView>
My question is: how to get text from textbox, while btnReject click action?
protected void btnReject_Click(object sender, EventArgs e)
{
LinkButton btnReject = (LinkButton)sender;
// how to get tbReason.Text from this item?
}
Regards
edit:
Problem resolved !
We just need to add in Page_Load code to prevent re-load listview and clear textboxes:)
http://forums.asp.net/t/1712482.aspx/1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvAttachments.DataSource = tAttachmentBO.getAttachmentsToAccept();
lvAttachments.DataBind();
}
}

Something like this should work
I'm on ipad so apologize for any mistakes
TextBox txt = (TextBox)btnReject.Parent.FindControl("tbReason")

Related

Asp.net web form passing argument on button click generated inside loop

I am generating buttons inside foreach loop
<% foreach (var myObject in myObjectList)
{
%>
<b>Text field</b>: <%= myObject.Text%><br>
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="pass myObjectText" Text="Delete"/>
<% } %>
It seems that I cannot use myObject's field inside CommandArgument however myObjectList is accessible
i.e
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObjectList.Count%>" Text="Delete"/>
The above statement would work but the one below would not
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObject.Text %>" Text="Delete"/>
Any idea why? And how can I pass myObject's field values as CommandArgument?
Update:
Button method in class
protected void WaitQueueDeleteBtn_Click(object sender, EventArgs e)
{
}
It's so easy with Repeater
Markup
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div><b>Text field</b>: <%# Eval("Text") %></div>
<asp:Button runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" />
</ItemTemplate>
</asp:Repeater>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Repeater1.DataSource = YOUR_DATA_SOURCE; // myObjectList
Repeater1.DataBind();
// ...
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandArgument == null) return;
var id = int.Parse(e.CommandArgument.ToString());
// your logic here ...
}
You can add multiple buttons and using CommandName can figure out which one clicked.
Hope this helps.

DataList SelectedIndexChange event not firing

I have hyperlink and a hidden field inside a datalist as shown below
<asp:DataList ID="clientsList" runat="server" OnSelectedIndexChanged="clientsList_SelectedIndexChanged1" >
<ItemTemplate>
<asp:HyperLink ID="hlName" runat="server" Text='<%# Bind("Name") %>' NavigateUrl="#" ></asp:HyperLink>
<asp:HiddenField ID="HiddenFieldID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:DataList>
When user clicks on any hyperlink, I need to store that value in an application variable. But the SelectedIndexChange event never fires.
This is my code:
protected void clientsList_SelectedIndexChanged1(object sender, EventArgs e)
{
int idx = clientsList.SelectedIndex;
HiddenField hiddenCID = clientsList.Items[idx].FindControl("HiddenFieldID") as HiddenField;
if (hiddenCID != null)
{
Logger.UpdateLog("Selected ID: " + hiddenCID.Value.ToString());
}
}
I am binding data from database to the datalist in the Page_load event as below and this is working fine.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
Any idea why this event is never fired?
Also is my code is right as far as accessing the hiddenfield value is concerned?
Have you set commandname property of hyperlink field?
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">Select</asp:LinkButton>
Now in selectedindexchanged method:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = DataList1.SelectedIndex;
Label lbl = (Label)DataList1.Items[idx].FindControl("Label1");
int id =Convert.ToInt32(DataList1.SelectedValue);
}
Does it work?

casting LinkButton Telerik

I have a LinkButton inside of a telerik grid when it is clicked it updates the database. The trouble comes at the first line where I recieve an error that I am unable to cast the radgrid to linkButton. Can someone shed a little light. Here is the Error Msg.
Telerik.Web.UI.RadGrid' to type 'System.Web.UI.WebControls.LinkButton
Here is my method:
protected void rad_grdCompleteRequest(object sender, EventArgs e)
{
LinkButton btnCompleteRequest = (LinkButton)sender;
int requestID = Convert.ToInt32(btnCompleteRequest.Attributes["RequestID"]);
SqlManager.UpdateRequest(requestID, 3);
Response.Redirect(Request.RawUrl);
}
Please try with the below code snippet.
.aspx
// Normal Mode
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
// Edit Mode
<telerik:GridTemplateColumn>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</EditItemTemplate>
</telerik:GridTemplateColumn>
.aspx.cs
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton LinkButton1 = sender as LinkButton;
// Do your logic here
}
Please let me know if you have any concerns.

Command Parameter TextBox: The name "X" does not exist in the current context

I get this message: The name "PINTextBox" does not exist in the current context.
..when I use this code
protected void SqlDataSource9_Selecting(object sender,
SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#PIN"].Value = PINTextBox;
}
The code works when I use a value of 3, but I want textbox values to be passed into the #PIN parameter and not just 3
protected void SqlDataSource9_Selecting(object sender,
SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#PIN"].Value = 3;
}
PINTextbox code is
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ID" GroupItemCount="2">
...
<EditItemTemplate>
...
<asp:TextBox ID="PINTextBox" runat="server" Text='<%# Bind("PIN") %>'/>
...
</EditItemTemplate>
...
</asp:ListView>
Is it giving me this message because the textbox is listed in SqlDataSource1 and not SqlDataSource9?
If so, is there any way for me to get around this still using two different data sources? Or how can I pass the data in PINTextBox to my command and into my select statement (not listed because it appears to work when I have the value 3 in the command)?
Thanks and let me know if you need more code.
You need to know which row's TextBox we're talking about.
//myRow contains the row that was just selected
TextBox txtPin = (TextBox)myRow.FindControl ("PINTextBox");
e.Command.Parameters["#PIN"].Value = txtPin.Text;
You can find the label/textbox using the listview's selectedindex and then extract the text as shown below.
(Not sure why you would like to use the textbox but you can use the label; the textbox code is commented).
You can replace "PINLabel" below with the actual ID for your label in ItemTemplate.
This code will be invoked the first time you load the page, hence you may need to take care of putting a condition for postback.
protected void SqlDataSource9_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//TextBox txtPIN = ListView1.Items[ListView1.SelectedIndex].FindControl("PINTextBox") as TextBox;
//e.Command.Parameters["#PIN"].Value = txtPIN.Text;
Label lblPIN = ListView1.Items[ListView1.SelectedIndex].FindControl("PINLabel") as Label;
e.Command.Parameters["#PIN"].Value = lblPIN.Text;
}
You need to ensure that this control is present in the SelectedItemTemplate as well like this:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ID">
......
<EditItemTemplate>
<tr>
......
<td><asp:TextBox ID="PINTextBox" runat="server" Text='<%# Bind("PIN") %>' /></td>
</tr>
</EditItemTemplate>
......
<ItemTemplate>
<tr>
......
<td><asp:Label ID="PINLabel" runat="server" Text='<%# Eval("PIN") %>' /></td>
......
</tr>
</ItemTemplate>
......
<SelectedItemTemplate>
<tr>
......
<td><asp:Label ID="PINLabel" runat="server" Text='<%# Eval("PIN") %>' /></td>
......
</tr>
</SelectedItemTemplate>
</asp:ListView>
Old Answer (IGNORE):
Use ".Text" for PINTextBox!
protected void SqlDataSource9_Selecting(object sender,
SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#PIN"].Value = PINTextBox.Text;
}

Access a control inside a the LayoutTemplate of a ListView

How do I access a Control in the LayoutTemplate of a ListView control?
I need to get to litControlTitle and set its Text attribute.
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Any thoughts? Perhaps via the OnLayoutCreated event?
Try this:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";
The complete solution:
<asp:ListView ID="lv" OnLayoutCreated="OnLayoutCreated" runat="server">
<LayoutTemplate>
<asp:Literal ID="lt_Title" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
In codebehind:
protected void OnLayoutCreated(object sender, EventArgs e)
{
(lv.FindControl("lt_Title") as Literal).Text = "Your text";
}
This technique works for template layout; use the init event of the control:
<asp:ListView ID="lv" runat="server" OnDataBound="lv_DataBound">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" OnInit="litControlTitle_Init" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
And capture a reference to the control for use in the code-behind (e.g) in the ListView's DataBound event:
private Literal litControlTitle;
protected void litControlTitle_Init(object sender, EventArgs e)
{
litControlTitle = (Literal) sender;
}
protected void lv_DataBound(object sender, EventArgs e)
{
litControlTitle.Text = "Title...";
}
For Nested LV Loop:
void lvSecondLevel_LayoutCreated(object sender, EventArgs e)
{
Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal;
litMainMenuText.Text = "This is test";
}
In case you need the VB version, here it is
Dim litControl = CType(lv.FindControl("litControlTitle"), Literal)
litControl.Text = "your text"

Categories

Resources