Pre-populate DetailsView InsertItemTemplate TextBox with User.Identity.Name value - asp.net

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

Related

Unable to get code behind event to fire

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

ASP.Net 4.0 OnSelectedIndexChanged event is not firing in ItemTemplate (Repeater)

Why I cant access OnSelectedItemChanged event? I already add AutoPostBack="true" in the textbox. Already tried to debug but still not firing.
Below are the sample codes:
<asp:Repeater runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Key") %>'></asp:Label><br />
<asp:CheckBoxList AutoPostBack="True" ID="CategoryAttributes"
runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Value") %>'
DataTextField="Text"
DataValueField="Value"
OnSelectedIndexChanged="OnSelectedIndexChanged">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
because the textbox is inside Repeater you need to use RepeaterItemEvent for example
in your markup
OnItemCommand="Rpt_ItemCommand"
in your codebehind
Protected Sub Rpt_ItemCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs)
'where your code goes
End Sub

Redirect to new page when user select from a dropdown list in ASP.NET

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

asp.net update pane will not fire event

I am trying to implement an update panel on my web page. when I add this, everything works fine:
<script runat="server">
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label2.Text = Date.Now.ToString
End Sub
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<fieldset>
<asp:Label ID="label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server" Height="289px" TextMode="MultiLine"
Width="663px" ReadOnly="True"></asp:TextBox>
The problem comes when I try to do some stuff on the on_load event of the application. in the code behind, i try to do this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
If Not IsPostBack Then
....a function that produces a very long xml string
Me.TextBox1.Text ="<f"
End If
End Sub
This will cause the event to not occur. If i change the "<" from the string, all is well. why cant I have "<" in my strings? This is important because i wont allow me to put an xml string in the text box.
any help would be great!
Try using &lt< rather than <

how to check which one has been clicked in asp.net

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>

Resources