asp vb gridview changes the input value by it self - asp.net

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:

Related

Return datalist linkbutton text value into a label

I have a datalist with a linkButton showing warehouse location numbers, when clicking on this datalist linkbutton I need the selected location number to show in a label. I am stuggling to make it happen; here is enter code heremy code:
<asp:DataList ID="LocList" runat="server" RepeatDirection="vertical"
RepeatColumns="18" RepeatLayout="Table" OnItemDataBound="DataList_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="lkLoc" runat="server" Text='<%# Eval("WHLOC") %>' ForeColor="#000" Font-Names="calibri" Font-Size="Medium" onclick="lk_Click" />
</ItemTemplate>
</asp:DataList>
<asp:Label ID="lbl1" runat="server"></asp:Label>
Protected Sub lk_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each dli As DataListItem In LocList.Items
Dim LkVal As String = CType(dli.FindControl("WHLOC"), LinkButton).Text.ToString
lbl1.Text = LkVal.ToString
Next
End Sub
Try with using below code it will helps you:
.aspx code:
<asp:DataList ID="LocList" runat="server" RepeatDirection="vertical"
RepeatColumns="18" RepeatLayout="Table" OnItemDataBound="DataList_ItemDataBound" OnItemCommand="DataList_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lkLoc" runat="server" Text='<%# Eval("WHLOC") %>' ForeColor="#000" Font-Names="calibri" Font-Size="Medium" onclick="lk_Click" />
</ItemTemplate>
</asp:DataList>
<asp:Label ID="lbl1" runat="server"></asp:Label>
.aspx.cs code here:
Protected Sub DataList_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
Dim lkLoc As LinkButton = CType(e.Item.FindControl("lkLoc"), LinkButton)
lbl1.Text = lkLoc.Text
End Sub

Error while binding data in GridView through Row Bound Event

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.

FindControl throwing NullReferenceException

Within my ASP gridview, I have the following (updated to show full gridview):
<asp:GridView ID="TPAnnuity_GridView" AllowSorting="true" AllowPaging="true" Runat="server"
DataSourceID="TPAnnuity_SqlDataSource" DataKeyNames="AnnuityTotalPointsID"
AutoGenerateColumns="False" ShowFooter="true" PageSize="20">
<Columns>
<asp:TemplateField HeaderText="Company" SortExpression="CompanyName" HeaderStyle-VerticalAlign="Bottom">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="EditACompanyID" runat="server" DataSource="<%# ddlCompanyDS %>" DataValueField="CompanyID" DataTextField="CompanyName" selectedValue='<%# Bind("CompanyID") %>'></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewCompanyID" runat="server" DataSource="<%# ddlCompanyDS %>" DataValueField="CompanyID" DataTextField="CompanyName"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="NewCompanyID" Display="Dynamic" ForeColor="" ErrorMessage="You must enter a value. *" Enabled="false"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<br />
<i>No Commission Data to display.</i>
<br />
<br />
</EmptyDataTemplate>
</asp:GridView>
And within my back end, I have the following:
Sub TPAnnuity_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles TPAnnuity_GridView.RowCommand
If e.CommandName = "Cancel" Then
'Reset Footer Row input fields
CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedIndex = 0
ElseIf e.CommandName = "Insert" Then
TPAnnuity_SqlDataSource.InsertParameters.Clear()
Dim test1 As New Parameter("CompanyIDInt", TypeCode.Int32)
test1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedValue
TPAnnuity_SqlDataSource.InsertParameters.Add(test1)
TPAnnuity_SqlDataSource.Insert()
ElseIf e.CommandName = "Update" Then
TPAnnuity_SqlDataSource.UpdateParameters.Clear()
Dim param1 As New Parameter("CompanyIDInt", TypeCode.Int32)
param1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("EditACompanyID"), DropDownList).SelectedValue ****THIS IS THE PROBLEM LINE****
TPAnnuity_SqlDataSource.UpdateParameters.Add(param1)
TPAnnuity_SqlDataSource.Update()
End If
End Sub
The Cancel and Insert functions work just fine, operating off of the footer. Every time hit the "Update" button, I get a NullReferenceException on the param1.Default Value = line.
Can anyone help me figure out what's going on here?
Your row should look like this:
param1.DefaultValue = CType(e.CommandSource.FindControl("EditACompanyID"), DropDownList).SelectedValue
Instead of utilizing the FooterRow, you have to use the source row. Since you passed that in with e, you can use this.

Why am I not getting any data back when I click on ImageButton in a GridView

I have the following Gridview, with an ImageButton in the last column. I am trying to return the First and Last names from the row from which the button has been clicked. I have spent several hours looking on here and other sites trying to get it to work, but with no success.
If someone could have a look at my code and see if I am doing anything wrong it would be much appreciated.
Thanks
ASP Code
<asp:GridView runat="server" ID="gvSecondaryContacts" AutoGenerateColumns="False" DataKeyNames="ContactID" ShowHeaderWhenEmpty="false" GridLines="None" OnRowCommand="gvSecondaryContacts_OnRowCommand" >
<Columns>
<asp:BoundField DataField="ContactID" HeaderText="ContactID" InsertVisible="False" ReadOnly="True" SortExpression="ContactID" Visible="false"/>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSecFirstName" Text='<%# Eval("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSecLastName" Text='<%# Eval("LastName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSecEmail" Text='<%# Eval("Email") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Position">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("ClubPosition") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSecClubPosition" Text='<%# Eval("ClubPosition") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibtnDelete" CommandName="Delete" AlternateText="Delete Contact" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
VB Code
Public Sub gvSecondaryContacts_OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Delete" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row = gvSecondaryContacts.Rows(index)
Dim ContactID As Integer = Convert.ToInt32(gvSecondaryContacts.DataKeys(index).Value)
Dim sFirstName As String = gvSecondaryContacts.Rows(index).Cells(1).Text
Dim sLastName As String = gvSecondaryContacts.Rows(index).Cells(2).Text
MsgBox("Your name is " & sFirstName & " " & sLastName)
End If
End Sub
I have also tried the following for the ImageButton but it returned is a type and cannot be used as an expression error
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibtnDelete" CommandName="Delete" AlternateText="Delete Contact" CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
You could hanlde the click-event instead. You can cast the ImageButon's NamingContainer to GridViewRow in the click-event handler. Then you just have to use FindControl:
Protected Sub Delete(sender As Object, e As EventArgs)
Dim ctrl = DirectCast(sender, Control)
Dim row = DirectCast(ctrl.NamingContainer, GridViewRow)
' you should rename this to LblFirstName
Dim Label2 = DirectCast(row.FindControl("Label2"), Label)
' you should rename this to LblLastName
Dim Label1 = DirectCast(row.FindControl("Label1"), Label)
'MessageBoxes in ASP.NET don't make much sense
MsgBox("Your name is " & Label2.Text & " " & Label1.Text)
End Sub
I think the problem is setting CommandArgument of ImageButton
CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"
You don't have do it according to this MSDN Example. Also you (if data binding done from server side) should bind your gridview only if page is not posting back as;
If Not Page.IsPostBack Then
'Bind your gridview here
End if
try this
Dim sFirstName As String = ((Label)gvSecondaryContacts.Rows(index).Cells(1).FindControl("Label2")).Text
Dim sLastName As String =((Label)gvSecondaryContacts.Rows(index).Cells(2).FindControl("Label1")).Text

Set textbox visibility based on dropdownlist value in gridview

I have a gridview that i would like to show or hide a text box based on the selected value of a dropdownlist on the same row.
My gridview:
<asp:GridView ID="GridViewUsers" runat="server" AutoGenerateColumns="False" CssClass="TableFramed">
<Columns>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true">
<asp:ListItem Value="1">Overtime</asp:ListItem>
<asp:ListItem Value="2">Temporary</asp:ListItem>
<asp:ListItem Value="3">Permanent</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="FNameValidator" runat="server" Text="*" ControlToValidate="txtFName" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
<asp:TemplateField HeaderText="hide me">
<ItemTemplate>
<asp:TextBox ID="txtHideMe" runat="server"></asp:TextBox>
</ItemTemplate>
</Columns>
</asp:gridview>
How do i wire it up so that the txtHideMe textbox can be hidden or displayed based off of the selected value of the dropdownlist?
Codebehide:
Protected Sub ddlType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each row In GridViewUsers.Rows
Dim reqType As DropDownList = CType(row.FindControl("ddlType"), DropDownList)
Dim txtHideMeAs TextBox = CType(row.FindControl("txtHideMe"), TextBox)
If reqType.SelectedItem.Value = "2" Then
txtHideMe.Visible = "False"
End If
Next
End Sub
Edit:
I would also like to be able to use a required field validator on the textbox if it's not hidden.
It looks like you need to bind the SelectedIndexChanged event to the control:
<asp:DropDownList runat="server" ID="ddlType" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" ...>

Resources