I have an asp.net datagrid which looks like this. I don't seem to be able to get the linkbuttons to fire.
<asp:DataGrid ID="dgrdItem" runat="server" OnItemDataBound="dgrdItem_ItemDataBound" Width="100%" AutoGenerateColumns="False" OnSelectedIndexChanged="EditItem" >
<HeaderStyle CssClass="datagridheaderstyle"></HeaderStyle>
<Columns>
<asp:TemplateColumn ItemStyle-Width="240" HeaderText="Name">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Name")%></ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Notes" HeaderText="Comments" ItemStyle-Width="180" />
<asp:TemplateColumn HeaderText="Options">
<ItemTemplate>
<table>
<tr>
<span class="infobarbutton">
<td id="tdEdit" runat="server">
<asp:LinkButton ID="btnEdit" Text="Edit" CommandName="Select" Width="52" runat="server" />
</td>
</span>
</tr>
</table>
</asp:TemplateColumn HeaderText="Options"></asp:DataGrid>
So the above is a stripped down version of the datagrid. Im then trying to fire the below handler which is all wired up correctly I believe.
Protected Sub EditItem(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'Do Some Work here...
End Sub
Any advice on where I am going wrong would be really helpful. Thank you.
You need to handle the ItemCommand event. Change OnSelectedIndexChanged="EditItem" to OnEditCommand="EditItem". Or you could just remove it from markup and add a handles clause like below:
Protected Sub EditItem(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdItem.ItemCommand
If e.CommandName = "Edit" ...
End Sub
Related
I created an asp.net web application (VB) with a dropdownlist that have lists of url to pages on the server. I am new to asp and VB. I have research different forums for solution and decided to ask for a specific solution to my problem.
Break down.
- I have a fully built page
- this page gets archive every two hrs to an archive folder(using a vbs)
- An XML file is generated with the file name and url (using a VBS)
- XMl is the datasource for the DDL.
What I want to accomplish is, when the user click an item from the DDL, they should be directed to that page.
After follow some of the suggestion from other forum and this one, nothing seems to work.
Once we dive into this, we will get some better understanding of any confusion.
The code-behind is VB, so i will prefered that language.
ASPX Page
enter code here
<%# Page Title="Home" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"CodeBehind="Default.aspx.vb" Inherits="Status._Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content><asp:Content ID="BodyContent"runat="server" ContentPlaceHolderID="MainContent"></asp:Content>
<asp:XmlDataSource ID="statsXML"
runat="server" DataFile="~/Archive/Stats.xml"
XPath="Elements/Element" />
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="statsXML"
DataTextField="Name"
DataValueField="Value"
AutoPostBack="True"
CssClass="rightCol" />
<br />
<p>
<asp:Table ID="Table1" runat="server" GridLines="Horizontal" Width="100%">
<asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12">
<asp:TableCell HorizontalAlign="Center" Text="Text here" BorderStyle="Solid" BorderWidth="0"
ForeColor="White" BackColor="#006699"></asp:TableCell>
</asp:TableRow>
<asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12">
<asp:TableCell HorizontalAlign="Center" Text="Text here" BorderStyle="Solid"
BorderWidth="0" ForeColor="White" BackColor="#006699"></asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
</p>
<asp:Table ID="Table2" runat="server" GridLines="both" Width="100%" BorderColor="Black">
<asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12" BorderColor="Black">
<asp:TableCell Width="50%" HorizontalAlign="Center" Text="Enviroment" BorderStyle="Solid" BorderWidth="1"
ForeColor="White" BackColor="#006699" BorderColor="Black"></asp:TableCell>
<asp:TableCell Width="50%" HorizontalAlign="Center" Text="State" BorderStyle="Solid"
BorderWidth="1" ForeColor="White" BackColor="#006699" BorderColor="Black"></asp:TableCell>
</asp:TableRow>
</asp:Table>`
Code-Behind
Public Class webform
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not Page.IsPostBack Then
'End If
'If Page.IsPostBack Then
' ' Response.Redirect(Me.DropDownList1.SelectedValue)
' End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Response.Redirect(DropDownList1.SelectedItem.Value)
End Sub
End Class
What you'll want to do is to first set AutoPostback to True on your DropDownList.
<asp:DropDownList ID="myDropDownlist" runat="server" AutoPostback="True" />
After that you should be able to handle the SelectedIndexChanged event for the DropDownList in your code-behind
Protected Sub myDropDownList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles myDropDownList.SelectedIndexChanged
' Lookup the selected item and redirect here
' (This assumes that you've bound your URL to redirect to the value of the item,
' and not the display text. Use SelectedItem.Text if the URL is being displayed
Response.Redirect(myDropDownList.SelectedItem.Value)
End Sub
Try This
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
Response.Redirect(DropDownList1.SelectedItem.Text)
End Sub
End Class
I am trying to put the UserName of the authenticated user on an Insert item template. I need the easiest explanation since I am very new. I really appreciate your help. This is what I have:
<InsertItemTemplate>
<asp:LoginName ID="LoginName2" runat="server" User.Identity.Name='<%# Bind("UserName") %>'/>
</InsertItemTemplate>
Thanks
Jose
Do this from the codebehind
<asp:DetailsView ID="DetailsView1" runat="server" OnDataBound="DetailsView1_DataBinding">
<InsertItemTemplate>
<asp:LoginName ID="LoginName2" runat="server" />
</InsertItemTemplate>
</asp:DetailsView>
Protected Sub DetailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBinding
LoginName2.text =User.Identity.Name
End Sub
inside of while loop, i've used
<asp:Button ID="<%=objReader.Item(0)%>" OnClick="btn_Click" runat="server" CssClass="submit_button" Text="Delete" />
Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
'You have clicked button
End Sub
to creating the button dynamically. Now, on click of particular button, it should show the information which are associated with the clicked button. Need Help !!
Solution
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="Jobs">
<ItemTemplate>
<asp:Button ID="btnDemo" CommandArgument='<%#Eval("Login_id")%>' OnCommand="btnDemo_Click" runat="server" Text="Button" /></ItemTemplate>
</asp:Repeater>
Sub btnDemo_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
MsgBox(e.CommandArgument)
End Sub
Thanks to every one!!
It's better to implement this in a databound control, like the repeater or listview.
You could then use the CommandArgument of a button to add some arguments which are distinct for each button.
You can then handle this in the Click procedure to handle the right action at the right CommandArgument.
see:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
Example:
<asp:Repeater runat="server" ID="rptList">
<ItemTemplate>
<asp:Button runat="server" ID="btnDemo" CommandArgument='<%#Eval("Id") %>' Text="Click me" OnCommand="btnDemo_Click" />
</ItemTemplate>
</asp:Repeater>
I have a GridView which you can click on a row and it should call the SelectedIndexChanged method (which updates another part of the page based on which row was selected). I have done something similar to this before and it worked, but I can't seem to get the SelectedIndexChanged part to be called for some reason.
The page is held in a master page which has a form runat="server" tag, and an <asp:ScriptManager> tag
I am using e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex)) to allow the SelectedIndexChanged to fire by clicking anywhere on the row.
To check that the code does work apart from that, I added a CommandField with a SelectButton and that successfully fires, but i would prefer to find a solution without having to use that.
code is below - any help would be appreciated. Thanks
GridView:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:HiddenField runat="server" ID="hdnScrollPosition" />
<asp:GridView ID="gridMessages" runat="server" CssClass="gridView" AutoGenerateColumns="False"
AllowPaging="true" GridLines="None" PageSize="10" ShowHeader="True"
EmptyDataText="--No Messages Received--" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
<ItemTemplate>
....
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.gridMessages.DataSource = ...
Me.gridMessages.DataBind()
End If
End Sub
Protected Sub gridMessages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMessages.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")
e.Row.Attributes.Add("onclick", "saveScrollPosition(); " & ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
End If
End Sub
SelectedIndexChanged (which never fires):
Protected Sub gridMessages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridMessages.SelectedIndexChanged
Response.Redirect("test.aspx")
End Sub
Don't you need a column CommandField defined as a SelectButton? Then, your markup would look something like:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:HiddenField runat="server" ID="hdnScrollPosition" />
<asp:GridView ID="gridMessages" runat="server" CssClass="gridView" AutoGenerateColumns="False"
AllowPaging="true" GridLines="None" PageSize="10" ShowHeader="True"
EmptyDataText="--No Messages Received--" Width="100%">
<Columns>
<asp:CommandField ShowSelectButton="true" ButtonType="Button" />
<asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
<ItemTemplate>
....
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
You didn't ask about this, but I always feel compelled to mention things like these two lines:
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")
This is a code smell. It's not a bad one, but mixing JavaScript attributes with VB/C# code is a habit you should break out of now. If you need to do something like this, add a CssClass property to your GridView and define those actions in CSS (or JavaScript/jQuery if CSS doesn't have enough events for you).
Edit:
Based on our discussion in comments, this looks like an inconsistency with the way the GridView can be modified. It may be related to the page/event lifecycle (somehow it's too late for ASP.NET to properly hook up the events?), and your best bet here is to switch from the SelectedIndexChanged event to SelectedIndexChanging.
I've been wrestling with the same issue...
It may not work in your scenario (or be a good thing to do at all) but try setting EnableEventValidation="false" for the page. This was the difference for me. Worked using either SelectedIndexChanged or SelectedIndexChanging events as mentioned above.
Use this:
e.Row.Attributes["onclick"]=ClientScript.GetPostBackEventReference(this.grvDetails,"Select$"+e.Row.RowIndex.ToString());
This is my Gridview code
<asp:DataGrid id="dg" runat="server" ondeletecommand="Delete_Item" >
<columns>
<asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove" />
</columns>
<HeaderStyle BackColor="#95C736" ForeColor="White" Font-Bold="True" />
</asp:DataGrid>
I want to replace my buttoncolumn with an image, what must I do to my GridView?
I would use a template column for this:
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/images/delete.png" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Replace the Button Column with a TemplateColumn with allows you to put standard asp controls inside. Then you can handle the Datagrid_ItemCommand event normally.
<asp:DataGrid ID="dgTest" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="ibtnDelete" runat="server" CommandName="cmdDelete"
ImageUrl="~/images/delete.png" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
The ItemCommand handler would be something like:
Protected Sub dgTest_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTest.ItemCommand
If (e.CommandName = "cmdDelete") Then
Response.Write("the command argument was :" & e.CommandArgument)
End If
End Sub
The only other thing you would need to do is bind some data to the image button for a command argument. I usually do something like:
Protected Sub dgTest_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim di As FAQTable = e.Item.DataItem
DirectCast(e.Item.FindControl("ibtn"), ImageButton).CommandArgument = di.FAQID
End If
End Sub
You could also use an asp:image control with an asp:Hyperlink control to get the same results.
One thing I just did that worked awesome is to put encoded html into the text. Text="<img src='images/btn-find.png' class='ttip' alt='View Details' />" This let me know only put in the img src, but also specify a class and an alt tag.
All you need to do is use single tick marks and encode your <> with gt and lt.