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
Related
I have a GridView GV_Edit_PatList, which has LinkButton btdEdit and Button lnkDelete. ASPX code is below.
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="lnkDelete" runat="server" ControlStyle-CssClass="dbody" CausesValidation="False" CommandName="DeletePatLab" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
Here I want when I click on LinkButton btdEdit
of any row in GridView Then Button lnkDelete should be disable of that row only. When I click on LinkButton btdEdit then event GV_Edit_PatList.RowEditing getting fired. So I wrote Code behind
mentioned below and also checked in debud mode lnkDelete.Enabled value is being set to False, Still It gets displaying on data grid:
Protected Sub GV_Edit_PatList_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GV_Edit_PatList.RowEditing
Try
GV_Edit_PatList.EditIndex = e.NewEditIndex
Dim lnkDelete As Button = CType(GV_Edit_PatList.Rows(Convert.ToInt32(e.NewEditIndex)).FindControl("lnkDelete"), Button)
lnkDelete.Enabled = False
BindGV_Edit_PatList()
Catch ex As Exception
Components.ExceptionLogger.HandleException(ex)
End Try
End Sub
I'm using asp.net web form gridview and I run into a problem when inserting a new record from footer.
my footer has the different input that should be submitted but when I submit the record they change.
for example, I have an input fname if I type the name "Issa" I get it like this ",Issa"
the "," character is being added and I don't know why
this is how I retrieve the record.
Dim fname As String = TryCast(staffs_gridvw.FooterRow.FindControl("footer_sfname_txtbox"), TextBox).Text.Trim
and this is how my templet field looks like.
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("sfname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="sfname_txtbox" CssClass="form-control" runat="server" Text='<%#Eval("sfname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="footer_sfname_txtbox" CssClass="form-control" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
any suggestion on how can I fix this issue???
Hum, I am un-able to re-produce this error.
My markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="sfname_txtbox" CssClass="form-control" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="footer_sfname_txtbox" CssClass="form-control" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" />
My code to fill:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid
End If
End Sub
Sub LoadGrid()
Dim rstData As DataTable
rstData = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
GridView1.DataSource = rstData
GridView1.DataBind()
End Sub
We now get this:
So I type in hello world into that text box, hit the button, with this code:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fname As String = TryCast(GridView1.FooterRow.FindControl("footer_sfname_txtbox"), TextBox).Text.Trim
Debug.Print(fname)
End Sub
And I see this:
So, I guess the question is what other operations are you doing to that GV?
Edit: Button is also in the the GV footer
Ok, so then we have this:
<div style="padding:25px;width:20%">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" CssClass="table">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="sfname_txtbox" CssClass="form-control" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="footer_sfname_txtbox" CssClass="form-control" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hotel">
<ItemTemplate>
<asp:Label ID="lblHotel" runat="server" Text='<%#Eval("HotelName") %>' ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
And code to load - same as before.
So, now we see this:
Of course, the button is now in the GV - so I can't double click to wire up a event, but you of course then use markup.
Type in onclick= (hit ctrl-space bar under the quotes) - and then you get this option:
Ok, so now we have our event behind.
This code:
Protected Sub Button2_Click(sender As Object, e As EventArgs)
Dim fname As String = TryCast(GridView1.FooterRow.FindControl("footer_sfname_txtbox"), TextBox).Text.Trim
Debug.Print(fname)
End Sub
And now this:
And click on button, I see/get this:
I have following gridview in aspx page:
<asp:GridView Runat="server" id="gv1" PageSize="20" Visible="False" AllowPaging="True" Width="100%"
CssClass="clsDataGrid" AutoGenerateColumns="false" DataKeyNames="intProofSLNo,txtAdminRemarks" CommandArgument='Eval(intProofSLNo,txtAdminRemarks)'
OnRowCommand="gv1_RowCommand" OnRowDataBound ="gv1_OnRowDataBound" >
<asp:BoundField DataField="intProofSLNo" ReadOnly="True" Visible="false" ItemStyle-Wrap="false" HeaderText="Sl No" ItemStyle-CssClass="clsNormalText"></asp:BoundField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnApprove" runat="server" CommandName="Approve" Text= "Approve / " />
<asp:LinkButton ID="lnkbtnReject" runat="server" CommandName="Reject" Text= "Reject" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Admin Remarks">
<ItemTemplate>
<asp:Label ID="lblAdminRemarks" runat="server" ItemStyle-Wrap="True" Text='<%# Bind("txtAdminRemarks")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAdminRemarksEdit" runat="server" cssclass=clsCommonInput MaxLength="252" Text='<%# DataBinder.Eval(Container.DataItem, "txtAdminRemarks")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lblEdit" runat="server" Text="Edit" ToolTip="Edit" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" Width="38" runat="server" Text="Update|" CommandName="Update"CommandArgument='<%# Container.DataItemIndex %>' CausesValidation="true" ToolTip="Save"> </asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Width="40" Text="Cancel" CommandName="Cancel"
CausesValidation="false" ToolTip="Reset"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The user clicks on the 'Edit' link which makes 'Update' and 'Cancel' links visible.It then enters the remarks in the textbox and clicks 'Update'.A row command event is fired which updates the remarks in DB for the entry in that particular row. The same event calls the bind function:
Protected Sub gv1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Update" Then
//some code to update remarks//
gv1.EditIndex = -1
subBindDataGrid()
End If
The subBindDataGrid() fires the following rowbound event which hides the buttons depending on admin remarks:
Protected Sub gv1_OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblAdminRemarks As Label = DirectCast(e.Row.FindControl("lblAdminRemarks"), Label)
Dim lnkReject As LinkButton = DirectCast(e.Row.FindControl("lnkbtnReject"), LinkButton)
Dim lnkApprove As LinkButton = DirectCast(e.Row.FindControl("lnkbtnApprove"), LinkButton)
Dim lnkEdit As LinkButton = DirectCast(e.Row.FindControl("lblEdit"), LinkButton)
If DataBinder.Eval(e.Row.DataItem, "txtAdminRemarks").ToString().Trim = "Auto-Approved" Then
lnkApprove.Visible = False
lnkReject.Visible = False
lnkEdit.Visible = False
Else
lnkbtnApprove.Visible = True
lnkbtnReject.Visible = True
End If
End If
The remarks get updated in the DB. However, there is some issue in binding and instead of displaying the grid, the page is redirected to an error page. There is no exception thrown anywhere while debugging. The same events are also called in 'Edit'command. However, the functionality works fine there.Please suggest if there is anything wrong with the syntax.
I see you don't define a DataSourceID on the GridView static definition, please make sure you define a DataSource for the GridView in the subBindDataGrid procedure, otherwise it will have a null datasource in input.
I have ListView1 with the following :
<ItemTemplate>
<tr style="text-align: center">
<td>Notification:
<asp:Label Text='<%# Eval("NotificationID") %>' runat="server" ID="NotificationIDLabel" Visible="False" />
<asp:Label Text='<%# Eval("CustomerID") %>' runat="server" ID="CustomerIDLabel" Visible="False" />
<asp:Label Text='<%# Eval("NotificationText") %>' runat="server" ID="NotificationTextLabel" /></td>
<td>
<asp:HyperLink ID="hlPromo" NavigateUrl='<%# Eval("URL") %>' ForeColor="#701A3C" runat="server">View</asp:HyperLink></td>
<td>
<asp:Button ID="btnDeleteNotification" runat="server" Text="Clear" ForeColor="#701A3C" BorderStyle="None" BackColor="#331700" /></td>
</tr>
</ItemTemplate>
I changed the btnDeleteNotification into a selected index change because I want to find out which NotificationID to delete in the SQL table. How could I grab the NotificationID of the selected row? I've tried every combination of VB I could think of to grab it.
Thanks in advance!
Use FindControl() inside of the SelectedIndexChanged event, like this:
Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim theNotificationLabel As Label = CType(ListView1.Items(ListView1.SelectedIndex).FindControl("NotificationIDLabel"), Label)
' Grab the ID from the text of the label
Dim theNotificationId As Integer = Convert.ToInt32(theNotificationLabel.Text)
End Sub
Note: If your list view is not named ListView1, then change it to whatever your list view is actually named.
I've been trying to make use of DetailsView for the past few days to show and edit data from a single record taken from a GridView, both read their datafrom an SqlDataSource.
I've been searching the internet and the MSDN for info about how to use a DetailsView to edit data and I managed to cobble something together, the problem is that apparently the actual updating method doesn't run.
So here's an extract of my code
GridView In the page:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DefaultMode="Edit">
<fields>
<asp:TemplateField HeaderText="Codice Cliente">
<ItemTemplate>
<asp:Label ID="lblCliCod" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_cod") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliCod" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_cod")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descrizione">
<ItemTemplate>
<asp:Label ID="lblCliDesc" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_desc")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliDesc" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_desc")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nome Utente">
<ItemTemplate>
<asp:Label ID="lblCliUser" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_user")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliUser" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_user")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblCliPass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_pass")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliPass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_pass")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amministratore">
<ItemTemplate>
<asp:Label ID="lblCliAdmin" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_admin")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkCliAdminE" runat="server" Text='<%# CBool(DataBinder.Eval(Container, "DataItem.cli_admin"))%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton Text="Aggiorna" ID="UpdateButton" runat="Server" CommandName="Update"></asp:LinkButton>
<asp:LinkButton Text="Elimina" ID="DeleteButton" runat="server" CommandName="Edit"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</fields>
</asp:DetailsView>
Relevant parts of the Codebehind:
Private Sub SetupDataSource() 'this is the sqldatasource I'm using for the detailsview
SqlDataSource2.ConnectionString = Assist.connectionString
SqlDataSource2.SelectCommand = detailsSelect
SqlDataSource2.SelectParameters.Add(New Parameter("id"))
SqlDataSource2.UpdateCommand = detailsUpdate
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_cod"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_desc"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_user"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_pass"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_admin"))
End Sub
Private Sub SetupDetailsView(id As Int32)
DetailsView1.AutoGenerateRows = False
DetailsView1.DataSource = SqlDataSource2
SqlDataSource2.SelectParameters("id").DefaultValue = id
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
cn.Open()
SetupDataSource()
If Not IsPostBack Then
Dim id As String = Request.QueryString("id")
If id <> "" Then
SetupDetailsView(id)
DetailsView1.DataBind()
End If
End If
End Sub
Protected Sub DetailsView1_ItemUpdating(sender As Object, e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim cliCod As String = (CType(DetailsView1.FindControl("txtCliCod"), TextBox)).Text.ToString()
Dim cliDesc As String = (CType(DetailsView1.FindControl("txtCliDesc"), TextBox)).Text.ToString()
Dim cliUser As String = (CType(DetailsView1.FindControl("txtCliUser"), TextBox)).Text.ToString()
Dim cliPass As String = (CType(DetailsView1.FindControl("txtCliPass"), TextBox)).Text.ToString()
Dim cliAdmin As Boolean = If((CType(DetailsView1.FindControl("chkCliAdminE"), CheckBox)).Checked = True, True, False)
SqlDataSource2.UpdateParameters("cli_cod").DefaultValue = cliCod
SqlDataSource2.UpdateParameters("cli_desc").DefaultValue = cliDesc
SqlDataSource2.UpdateParameters("cli_user").DefaultValue = cliUser
SqlDataSource2.UpdateParameters("cli_pass").DefaultValue = cliPass
SqlDataSource2.UpdateParameters("cli_Admin").DefaultValue = If(cliAdmin = True, "1", "0")
DetailsView1.DataBind()
End Sub
I'm either missing something or doing it wrong somewhere, which one is it and why?
I'm not an expert at using SqlDataSource, but you might try inserting a SqlDataSource.Update in your Item_Updating method:
SqlDataSource2.UpdateParameters("cli_cod").DefaultValue = cliCod
SqlDataSource2.UpdateParameters("cli_desc").DefaultValue = cliDesc
SqlDataSource2.UpdateParameters("cli_user").DefaultValue = cliUser
SqlDataSource2.UpdateParameters("cli_pass").DefaultValue = cliPass
SqlDataSource2.UpdateParameters("cli_Admin").DefaultValue = If(cliAdmin = True, "1", "0")
SqlDataSource2.Update();
DetailsView1.DataBind()
I use a DetailsView in an ongoing project. They are finicky, but they work great when set up correctly.
Several potential issues I can see with your DetailsView are:
Eval vs Bind
As with all databound controls, Databinder.Eval is a read-only databind method. To allow controls to automatically update, you must use Bind in the edit template. Of course, you can manually update your database, but why reinvent the wheel?
<asp:TemplateField HeaderText="Codice Cliente">
<ItemTemplate>
<asp:Label ID="lblCliCod" runat="server" Text='<%# Eval("DataItem.cli_cod") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliCod" runat="server" Text='<%# Bind("DataItem.cli_cod")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Built-in Update functionality vs manual update functionality
You don't really need to use DetailsView1_ItemUpdating unless you are want to catch and modify values after submit and before committing to the datasource. i.e. concatenating individual year/month/day textboxes into a single date.
However, since you are not currently generating an Update button (using property AutoGenerateEditButton="True"), you may need to handle the update manually as you are attempting. Alternatively, for basic binding and update, I would comment out the updating functions, add the autogenerateeditbutton="True" property, and then try updating.
Primary Key
With some datasources, you must specify the DataKeyNames property (your primary key/s of your datasource) on the DetailsView to enable auto-binding. I don't know for sure, but you may also need to set the DefaultMode property.
All properties and methods are explained well on the MSDN DetailsView page, but it's rather verbose. The MSDN Updating a DetailsView page is more concise