I have a gridview with 100 Textboxes one below the other. I want to validate it as Atleast One textbox should be filled, otherwise Save button should not work and Ask for user to enter atleast one textbox. Is there any ways to do this?In my case, Gridview Textboxes Have same id as 'txtempcode'.
I'm using vb.net with SQl database as backend.
Here is my Design code
<asp:GridView ID="GridView2" runat="server" style="margin-left: 23px" Width="420px" CellPadding="4" AutoGenerateColumns="False" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField Headertext="Sr No." DataField="Row_No" />
<asp:TemplateField HeaderText="Employee Code">
<ItemTemplate>
<asp:TextBox ID="TxtEmpcode" runat="server" OnTextChanged="TxtId_TextChanged" AutoPostBack ="true" ></asp:TextBox>
</Columns>
</asp:GridView>
In your Save button's click event:
Dim bValueFound as Boolean = False
for each i as listitem in GridView2.items
try
if DirectCast(i.findControl("TxtEmpcode"), TextBox).text > "" then
bValueFound = True
exit for
end if
catch
'Hopfully just Header
end try
next
If bValueFound then
'Do Save Button Tasks
else
'Remind user to fill something in?
End If
You may want to do more than just ...text > "" but I'll leave that up to you.
Related
I created a simple Grid view with Multiple columns and add a button so when clicked pass the values of the row to a different webform but button on click not firing. I use the example code from the link
: http://msdn.microsoft.com/en-us/library/bb907626(v=vs.100).aspx
And this is my code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
PageSize="100" AutoGenerateColumns="False">
<Columns>
<%--<asp:HyperLinkField HeaderText="Edit" NavigateUrl="FormReport2.aspx"
Text="Edit" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="fldEmployeeID" HeaderText="EmployeeID"
SortExpression="fldEmployeeID" />
<asp:BoundField DataField="fldAbsentDate" HeaderText="AbsentDate"
SortExpression="fldAbsentDate" />
<asp:BoundField DataField="fldAbsentCode" HeaderText="AbsentCode"
SortExpression="fldAbsentCode" />
<asp:BoundField DataField="fldRuleViolationWarningType"
HeaderText="Rule Violation Warning Type"
SortExpression="fldRuleViolationWarningType" />
<asp:BoundField DataField="fldRuleViolationIssueDate"
HeaderText="Rule Violation Issue Date"
SortExpression="fldRuleViolationIssueDate" />
<asp:BoundField DataField="fldLOAEndDate" HeaderText="LOA End Date"
SortExpression="fldLOAEndDate" />
</Columns>
</asp:GridView>
code behind
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "AddToCart") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button
' from the Rows collection.
Dim row As GridViewRow = GridView1.Rows(index)
' Add code here to add the item to the shopping cart.
End If
End Sub
Any help would be very appreciated. And also what would be the best way to pass the values from the row to a different web form from the code behind?
You forgot to sing up for the event. See the last line here:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
PageSize="100" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand">
I am very much new to vb.net.
I am trying to display a Listview from my DB. As show below.
companyID Username
3456 raj
7965 king
But I dont how add a Button or a link as show below
companyID Username
3456 raj Accept/Decline
7965 king Accept/Decline
So when the user select the Accept then I need to get the Company ID and Username and sent do some insert to another table. And if it selects the Decline just I need to delete it from DB.
I have no problem with writing insert or delete Queries But my problem is how implement Accept or Decline Button or text link and get all the details.
So can any one tell me how to do it.
An alternative to ListView would be using GridView with template columns, try this:
In Markup create the GridView:
<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center" DataKeyNames="CompanyIDColumn" Width="100%" >
<Columns>
<asp:BoundField DataField="CompanyIDColumn" HeaderText="Company ID" />
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="UserName" Text='<%# Eval("YourUserNameColumn")%>' runat="server"/>
</ItemTemplate>
<HeaderTemplate>UserName</HeaderTemplate>
<HeaderStyle Width="300px" />
<ItemStyle Width="300px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="BtnDecline" CommandName="DeclineCommand" OnCommand="BtnDecline_click" CommandArgument='<%# Eval("CompanyIDColumn")%>' runat="server">Accept</asp:LinkButton>
</ItemTemplate>
<HeaderTemplate></HeaderTemplate>
<HeaderStyle Width="50px" />
<ItemStyle Width="50px"/>
</asp:TemplateField>
</Columns>
</asp:GridView>
After you finish the gridView binding create the following BtnDecline_Click function in Code Behind:
Protected Sub BtnDecline_click(ByVal sender As Object, ByVal e As CommandEventArgs)
'The company ID in the same row in which the user clicked
Dim CompanyID As String
CompanyID = e.CommandArgument.ToString()
'You can now use the company id in any opertations
End Sub
Not sure if using a listView is a must for you, but this works great for me.
I am new to web dev and using controls so please forgive me.
I have a GridView with check boxes in it (please see markup below)
When a user goes through and checks any boxes and hits my submit button
I want to run LINQ query to get all the rows with the checkbox1.checked = True
Something like:
Dim sList = (From row in Gridview1
Where row.Cells("IsStarFleet") = True
row.Cells("ID)).ToList
Markup:
<asp:GridView ID="GridView1" runat="server" Width="516px" AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:TemplateField HeaderText="IsStarFleet">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack ="False" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You need to use FindControl for controls in a TemplateField and Cells(index) for BoundFields:
Dim checkedIDs = From row In GridView1.Rows.Cast(Of GridViewRow)()
Where DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
Select row.Cells(0).Text
Dim checkedIdList = checkedIDs.ToList()
i am using asp.net for my web application when i click on checkbox inside gridview and after that i check its value on button click it does not show me the exact value
here is asp code
<asp:GridView ID="dgvMenu" runat="server" Width="100%" CssClass="grid" GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
View
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="View" runat="server" Checked="<%#Bind('View') %>" />
</ItemTemplate>
<ItemStyle Width="50px" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Menu" HeaderText="Menu Name">
<HeaderStyle HorizontalAlign="Left" CssClass="firstcol" />
<ItemStyle CssClass="firstcol" />
</asp:BoundField>
</Columns>
</asp:GridView>
and here is its vb version to get its value on button click version
For Each item As GridViewRow In dgvMenu.Rows
Dim MenuName As String = item.Cells.Item(1).Text
Dim chkView As CheckBox = DirectCast(item.FindControl("View"), CheckBox)
Next
i want to check its value whether its checked or unchecked so that i can process its value
I assume that your binding your GridView even on postbacks. You should only bind it if not PageIsPostback.
Put this into Page_Load:
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' following function loads data and binds it to the GridView '
BindGrid()
End If
End Sub
If you DataBind the Grid on every postback, ViewState cannot be reloaded, hence all changed values are lost and events will not be triggered correctly.
I have a Checkbox Control (column) in a header template field, for a Gridview control. On page load the gridview reads data from the server for each row. What I want is, when the header Checkbox control is checked, it updates the each row to either true or false (checked or unchecked) based on the checked state of the header column. The thing is after its checked, the header Checkbox it doesn't keep the checked state, so if I checked it, after posting the data back to the server it defaults to unchecked, I want it to keep it state, so I can uncheck it and save back to the database.
Here is the Code from the my .ASPX page
<asp:GridView ID="CategoriesGridView" runat="server" AutoGenerateColumns="false" EmptyDataText="No files Uploaded" BorderWidth="1px" BackColor="White"
AllowPaging="False" CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC" Font-Names="Arial"
DataKeyNames="ID">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" Text="Show Discount" ItemStyle-Width="110" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkShowDiscountImage" runat="server" Checked='<%# Eval("ShowDiscountImage")%>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" />
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
And here is the code form the code behind for the checkbox
Protected Sub chkAll_CheckedChanged(sender As Object, e As EventArgs)
Dim ChkBoxHeader As CheckBox = CType(CategoriesGridView.HeaderRow.FindControl("chkAll"), CheckBox)
'Enumerate each GridViewRow
For Each gvr As GridViewRow In CategoriesGridView.Rows
'Programmatically access the CheckBox from the TemplateField
Dim cb As CheckBox = CType(gvr.FindControl("chkShowDiscountImage"), CheckBox)
If ChkBoxHeader.Checked = True Then
cb.Checked = True
Else
cb.Checked = False
End If
Next
Save()
End Sub
Protected Sub Save()
Dim result As Boolean = False
For Each row As GridViewRow In CategoriesGridView.Rows
' Get the Id from the DataKey property.
Dim cId As Integer = Convert.ToInt32(CategoriesGridView.DataKeys(row.RowIndex).Values(0))
'Get the checked value of the CheckBox
Dim showDiscountImage As Boolean = TryCast(row.FindControl("chkShowDiscountImage"), CheckBox).Checked
result = CatalogServices.Categories.UpdateCategory(cId, showDiscountImage)
Next
If result = True Then
PopulateGridView()
Else
'ToDo Display Error of some sort
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Alert", "alert('There was a problem updating the database')", True)
End If
End Sub
Instead of using Page_Load use the Page_Init event.