How to get DataList's ItemTemplate items Url on server - c#

I am using Asp.Net C# 4.0. I am using DataList on a page which include ImageButton. The code is as follows;
<asp:DataList ID="DataList1" runat="server" DataSourceID="ObjectDataSource2"
DataKeyField="Pic_ID">
<HeaderTemplate>
Click Image to enlarge
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Height="110px"
ImageUrl='<%# Eval("Pic_Path") %>' Width="111px"
onclick="ImageButton1_Click(this.ToString())"/>
</ItemTemplate>
</asp:DataList>
I want to send the current ImageUrl in the ImageButton to a method "ImageButton1_Click" on server in code behind. I tried changing my code several times but could not fix it. With this code i am currently getting error which says "Method name expected". My method on server is;
protected void ImageButton1_Click(string Param)
{
//Code here
}
Saying in short, I want to send the Url of the image in DataList to server for some processing. Thanks for any help.

try below
<asp:ImageButton ID="ImageButton1" runat="server" Height="110px"
ImageUrl='<%# Eval("Pic_Path") %>' Width="111px"
onclick="ImageButton1_Click"/>
and change method as
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
var btn = sender as ImageButton;
var url = btn.ImageUrl;
}

Related

Get Column value 'TemplateField' of Each row in Button Click event

I have created a gridview using TemplateField. And Added Add Button And this redirects to other page and i want to get the one column value of row.
Here is the code -
<Columns>
<asp:TemplateField HeaderText="Accession ID">
<ItemTemplate>
<asp:Label Text='<%# Eval("AccessionID") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" Text="Hello" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstNameFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
......
<asp:TemplateField HeaderText="Add Container">
<ItemTemplate>
<asp:Button ID="btnAddContainer" runat="server" CommandName="" Text="Add Container" CommandArgument='<%# Eval("AccessionID") %>' OnClick="ContainerforAccession" class="btn btn-primary btn-md" />
</ItemTemplate>
</asp:TemplateField>
CS Code -
protected void ContainerforAccession(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
}
I have this code with me. I want to get first column value (AccessionID) in my case in every button click of the Grid.
I have tried across multiple solutions. But unfortunately, we have the answers for 'asp:BoundField' Implementation. I am looking for 'asp:TemplateField'
Please let me know if i need to convert to asp:BoundField. If so, How can the above design can be changed - Binding Text='<%# Eval("AccessionID") %> to Textbox.
Thanks in advance. Looking for a solution.
Change the OnClick of the button to OnCommand. Then you have access to the CommandEventArgs.
<asp:Button ID="btnAddContainer" OnCommand="ContainerforAccession"
protected void ContainerforAccession(object sender, CommandEventArgs e)
{
string AccessionID = e.CommandArgument.ToString();
}
Changed All the Template fields to Bound-Fields and in cs code -
int AccessionID = Int32.Parse((sender as Button).CommandArgument);
Solved my issue !

Open Ajax TabContainer tabs using query string in Asp.net

Is it possible to open tabs of Ajax Tab Container using query string.
Something like when the query string is
localhost:81/dashboard.aspx?tab=0
localhost:81/dashboard.aspx?tab=1
localhost:81/dashboard.aspx?tab=3
My Code is
<ajax:TabContainer ID="TabContainer2" runat="server" CssClass="MyTabStyle">
<ajax:TabPanel ID="TabPanel2" runat="server" TabIndex="0">
<headertemplate>
Overview
</headertemplate>
<contenttemplate>
</contenttemplate>
</ajax:TabPanel>
<ajax:TabPanel ID="tbpnluser1" runat="server" TabIndex="1">
<headertemplate>
Overview
</headertemplate>
<contenttemplate>
</contenttemplate>
</ajax:TabPanel>
</ajax:TabContainer>
Please help
YES there is property called ActiveTabIndex
by using it you can show the tab you want in server-side
in server-side
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(Request.QuerryString["tab"]==1)
{
tabcantainerID.ActiveTabIndex =1
}
.....
}
}
hope this will help you

how to get value of textbox in listview?

<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")

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;
}

Categories

Resources