How to make linkbutton control in gridview open an new ie window - asp.net

I need to
<asp:TemplateField HeaderText ="ename">
<ItemTemplate > <asp:Label ID="lbl2" Text ='<%#Eval("ID") %>' runat ="server" >
</asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="textbox1" Text='<%#Eval("name")%>' runat ="server" ></asp:TextBox>
<asp:LinkButton ID ="link1" Text='<%#Eval("name")%>' runat ="server" ></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
i have an textbox and link button in Edit itemtemplate
based on the condition()
if(Text ='<%#Eval("ID") %>')
id=1 show textbox[edit item temalpate]
id=2 show link button[edit item temalpate]
id=3 show link button[edit item temalpate]
now in link button i have value [Text='<%#Eval("name")%>'] (eg:www.stackoverflow.com ,google.com)
so that one an user clicks on the link button open a new browser window and show that website show to open an new browser window on the clcik of the link button

Could you use a HyperLink control rather than a LinkButton?
eg
<asp:HyperLink id="hyperlink1"
NavigateUrl="<%#Eval('name')%>"
Text="<%#Eval('name')%>"
Target="_blank"
runat="server"/>

You can just bind to the OnClientClick event of the LinkButton. I would do all of this by implementing the OnDataBinding event for that control:
Eg:
// In your .aspx
<asp:LinkButton ID ="yourButton" runat="server" OnDataBinding="yourButton_DataBinding" />
//In your .cs
protected void yourButton_DataBinding(object sender, System.EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
btn.Text = Eval("name");
btn.OnClientClick = string.Format("window.open('{0}', 'yourNewWindow'); return false;", Eval("name"));
}
If you need more info on how the javascript window.open works check out this link:
http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

<asp:HyperLink id="hyperlink1" NavigateUrl="<%#Eval('name')%>" Text="<%#Eval('name')%>" Target="_blank" runat="server" />
before NavigateUrl we need to the code "http" as shown then it woks fine
NavigateUrl='<%# "http://" + Eval('name')%>'

can Add
OnClientClick="aspnetForm.target ='_blank';"
so on click it will call Javascript function an will open respective link in News tab.
<asp:LinkButton id="lbnkVidTtile1" OnClientClick="aspnetForm.target ='_blank';" runat="Server" CssClass="bodytext" Text='<%# Eval("newvideotitle") %>' />

Related

How do I open DropDown of RadComboBox on asp Button click?

How do I open DropDown of RadComboBox on asp:Button click? Both RadComboBox and Button are inside EditItemTemplate of RadGrid.
My requirement is: I had to open DropDown of RadComboBox ("ddlAccountCode" in below HTML code) on a click of button ("btnSearch" in below HTML code).
Below is the HTML code of RadComboBox and Button
<telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
<ItemTemplate>
<asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false"></asp:Label>
<telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310" HighlightTemplatedItems="true" CausesValidation="true"
OnItemsRequested="ddlAccountCode_ItemsRequested" EnableItemCaching="true" ShowDropDownOnTextboxClick="false" EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true" MarkFirstMatch="True" AllowCustomText="true"
Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID" AutoPostBack="true" OnSelectedIndexChanged="ddlAccountCode_SelectedIndexChanged">
</telerik:RadComboBox>
<telerik:RadButton id="btnSearch" runat="server" text="Search" OnClick="btnSearch_Click">
</telerik:RadButton>
</EditItemTemplate>
</telerik:GridTemplateColumn>
I am doing searching/filtering in RadComboBox on a button click. All is working fine except when I type/search anything in textbox of RadComboBox and click on button to search, the dropdown of RadCombo does not open up. Due to this, every time, I have to manually open the dropdown to see the result of searched text in RadComboBox.
Below line of code is working fine for my requirement:
protected void btnSearch_Click(object sender, EventArgs e)
{
GridEditableItem editedItem = (sender as Button).NamingContainer as GridEditableItem;
RadComboBox combo = (RadComboBox)editedItem.FindControl("ddlAccountCode");
combo.OpenDropDownOnLoad = true; // opens dropdown of RadComboBox
}

How to fetch the Value of a LinkButton in a GridView Template Field

I Have a LinkButton In a TemplateField In a GridView. The LinkButton is bound to database which shows the time save in database.All I want is to fetch each linkbutton time value and compare it to current time and if linkbutton time is less than current time ,make it disable.
Any pointers will be helpful.
Thanks in advance.
<asp:GridView ID="grdview" AutoGenerateColumns="false" runat="server" OnRowCommand="grdview_RowCommand">
<Columns>
<asp:BoundField DataField="AudiName" DataFormatString="Audi {0}" HeaderText="Audi Name" />
<asp:TemplateField HeaderText="StartTime">
<ItemTemplate>
<asp:LinkButton ID="lnkmovietime" runat="server"
Text='<%# Eval("StartTime") %>'
CommandName="time"
CommandArgument='<%#Eval("AudiID") %>'>
</asp:LinkButton>
<asp:HiddenField runat="server" ID="hf1" Value='<%#Eval("StartTime")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use the OnRowDataBound event to hook into the data binding and disable the button.
Form
<asp:GridView ID="MyGridView"
OnRowDataBound="MyGridView_RowDataBound"
runat="server">
....
</asp:GridView>
CodeBehind
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find the button
var linkButton = (LinkButton) e.Row.FindControl("MyLinkButton");
// Toggle enabled/disabled based on time
// Using the button text is probably a bad idea, but is used here for demonstration purposes
linkButton.Enabled = (Convert.ToDateTime(linkButton.Text) > DateTime.Now);
}
}
The code above has not been tested, but should give you an idea of how you can approach this.
You can set the Enabled property of LinkButton comparing with the current time:
<asp:LinkButton ID="lnkmovietime" runat="server"
Text='<%# Eval("StartTime") %>'
Enabled = '<%# ((DateTime)Eval("StartTime")< DateTime.Now )? false : true %>'
CommandName="time" CommandArgument='<%#Eval("AudiID") %>'>
</asp:LinkButton>

Add a clickable image to a GridView in ASP.Net

I'm trying to add an delete image at the end of each row in a GridView. I want the user to be able to click the image to delete the row.
So I've used a HyperLinkField to create a link to another page which will delete the record:
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="RemoveLine.aspx?ID={0}"
Target="_blank" />
The HyperLinkField doesn't contain an Image tag so I created a TemplateField with an Image inside.
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imageRemove" runat="server" ImageUrl="~/Images/smallcross.gif" />
</ItemTemplate>
</asp:TemplateField>
However the HyperLinkField and Image appear in different columns and the image has no click event.
Any way of combining the two?
I'm using ASP.Net 4.0.
Thanks in advance
Normally you need to identify which record you want to delete, you can use the CommadArgument property to identify the record's Id:
<asp:TemplateField HeaderStyle-Width="40">
<ItemTemplate>
<asp:ImageButton ID="ButtonDelete" runat="server"
ImageUrl="~/Imags/delete.png" OnClick="ButtonDelete_Click" ToolTip="Delete"
CommandArgument='<%#Bind("UserId")%>'/>
</ItemTemplate>
</asp:TemplateField>
protected void ButtonDelete_Click(object sender, EventArgs e)
{
ImageButton button = sender as ImageButton;
DeleteUserById(Convert.ToInt32(button.CommandArgument));
}
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server"
ImageUrl="~/Imags/delete.png" OnClick="btnDelete_Click"
ToolTip="Delete row" CommandName="Eliminar" CommandArgument='<%#Eval("UserId")%>'/>
</ItemTemplate>
You can use the CommandArgument to pass the ID value of the selected row and perform
the desires results
// fires when the ImageButton gets clicked
protected void GridView1_ItemCommand(object sender, DataGridCommandEventArgs e)
{
if(e.Commandname ="Eliminar"){
this.Eliminar(Convert.ToInt32(e.CommandArgument));
}
}
// function to delete the record
private void Eliminar(int code)
{
//custom code to delete the records
}
How about an image button?
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/Images/smallcross.gif" onclick="ImageButton1_Click" />
</ItemTemplate>
you can wrap your image with a raw anchor tag , you get the same result.
<asp:TemplateField>
<ItemTemplate>
<a href="RemoveLine.aspx?ID={0}">
<asp:Image ID="imageRemove" runat="server" ImageUrl="~/Images/smallcross.gif" />
</a>
</ItemTemplate>
</asp:TemplateField>

Getting textbox value inside a Detailsview control

I have a DetailsView control with a template field as follows:
<asp:TemplateField SortExpression="Title">
<ItemTemplate>
<asp:TextBox ID="txtMessageTitle" Text='<%# Bind("Title") %>' runat="server">
</asp:TextBox>
<asp:Button ID="btnUpdateTitle" runat="server" Text="Update"
CommandName="UpdateTitle" CommandArgument='<%# Bind("MessageID") %>' oncommand="btnUpdateCommand"/>
</ItemTemplate>
</asp:TemplateField>
The Details View is wrapped inside an UpdatePanel.
When the user clicks on btnUpdateButton I'd like to be able to retrieve the textbox (txtMessageTitle) value in code behind and by using CommandArgument of the button, update the appropriate MessageTitle in database. How can I retrieve the textbox value inside the DetailsView control from the Command event of my button?
Thanks.
use the following:
TextBox txtMessageTitle = detailsViewName.FindControl("txtMessageTitle") as TextBox;
string text = txtMessageTitle.Text;

How set panel Default Button that is inside a details view in asp.net?

<asp:panel ID="Panel1" runat="server">
<asp:DetailsView ID="DetailsView1"
....
<asp:templatefield ShowHeader="False">
<insertitemtemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert"></asp:Button>
...
<asp:DetailsView>
</asp:panel>
and i write the code for setting the panels default button in details view's DataBound event
Button btnAdd = new Button();
btnAdd = DetailsView1.Rows[indexNumber].FindControl("btnAdd") as Button;
Panel1.DefaultButton = btnAdd.UniqueID;
but I get the error :
The DefaultButton of 'Panel1' must be the ID of a
control of type IButtonControl.
In ASP you set the id as
ID="BtnAdd"
In code you use
FindControl("btnAdd")
btnAdd is case sensitive I believe

Resources