[Asp.net / Vb.Net]
How can I validate my gridview footer checkbox using Javascript.
I have a gridview with footer having 8 checkboxes -(chkActive1,chkActive2,.....chkActive3) -
The condition I want to jave is - The user need to check atleast 1 checkbox or else the trasaction would not allow
thanks..
so simple, every check box have unique id check in view source there is some patent in name in controls when you check viewsource of that page, just use that, its simple way.
If I misunderstood you then please correct me, I think this could be done via using a custom validator in asp.net
Since you haven't mentioned I am assuming you are using CheckBoxList Control.
On .aspx page use custom validator
<asp:CustomValidator ID="customValidatorForCheckboxlist" runat="server"
ErrorMessage="Required Field" ValidationGroup="valSurvey"
OnServerValidate="CheckifCheckBoxIsEmpty" SetFocusOnError="true" Display="Dynamic"></asp:CustomValidator>
On codebhind using this logic to iterate through your gridview row and check for footer row. Inside footer row iterate through all checkboxes and then see if they are selected or not. I am using a boolean field to check if anything was selected. Jump out if anything is selected.
Protected Sub CheckifCheckBoxIsEmpty(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
Dim valbool As Boolean = False
For Each gvrow As GridViewRow In gridview_1.Rows
'Check for footer row.
If gvrow.RowType = DataControlRowType.Footer
For Each ct As Control In gvrow.Cells(1).Controls
If ct.GetType.ToString().Equals("System.Web.UI.WebControls.CheckBoxList") Then
Dim _checkboxlist As CheckBoxList = DirectCast(ct, CheckBoxList)
For Each ListItem1 As ListItem In _checkboxlist.Items
If ListItem1.Selected = True Then
valbool = True
Exit For
Else
valbool = False
End If
Next
End If
Next
End If
Next
If valbool = False Then
e.IsValid = False
Else
e.IsValid = True
End If
End Sub
Related
I am trying to check checkboxes on certain rows of a gridview based off of a selection from a dropdown. I have a gridview where the first column is a checkbox (cb_pain), and the second column is an ID (DrugID). The user makes a selection from the dropdown list, this fires a stored procedure. The stored procedure returns all of the DrugID's that need to be checked. I'm able to pull the DrugID from the data like this: dt.Rows(0)("pain1").ToString() That tells me DrugID for a row that needs to be checked.
Basically I would like to check the checkbox on the row where DrugID = dt.Rows(0)("pain1").ToString()
I think this needs to be done on the selectedindexchange of the dropdown.
My gridview looks like this: (sorry for the dot's, I couldn't figure out how to tab)
cb_pain........DrugID...............Field1
x.................3...................other data
x.................23.................other data
x.................24.................other data
x.................37.................other data
How can I use this to check the checkbox on the row that has the right DrugID?
I've tried a couple of different DirectCast things, but no success. Can anyone point me in the right direction?
Thanks
Another option that I tend to prefer is to make use of the RowDataBound event of the GridView. As each row gets data bound to it, this event gets called. What you would do is find the DrugID control and check its value. If it is what you want, do something.
First, let your GridView know of the event.
<asp:GridView ID="gvDrugs" runat="server" OnRowDataBound="gvDrugs_RowDataBound">
</asp:GridView>
Then handle the event. If the DrugID is 23, find the CheckBox and check it.
Protected Sub gvDrugs_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblDrugID As Label = e.Row.FindControl("lblDrugID")
If lblDrugID.Text = "23" Then
Dim cbPain As CheckBox = e.Row.FindControl("cbPain")
cbPain.Checked = True
End If
End If
End Sub
**Note here that I am assuming the types of controls to be labels and checkboxes. You have not shown your markup, so this is the best I can do.
After your edit, I would probably agree with you. This is something that could be done in the SelectedIndexChanged event of the DropDownList. Get the list of DrugIDs that need to be checked and iterate each row of your GridView, checking those that match.
Protected Sub dropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim uniqueDrugIDs As HashSet(Of String) = New HashSet(Of String)()
' Here we assume dt is some sort of global variable as your question above implies
For Each dr As DataRow In dt.Rows
uniqueDrugIDs.Add(dr("drugID").ToString())
Next
For Each gvRow As GridViewRow In gvDrugs.Rows
Dim lblDrugID As Label = gvRow.FindControl("lblDrugID")
If (uniqueDrugIDs.contains(lblDrugID.Text)) Then
Dim cbPain As CheckBox = gvRow.FindControl("cbPain")
cbPain.Checked = True
End If
Next
End Sub
You can go for that kind of loop that will go through all your rows :
for (int x = 0; x < NameOfYourGrid.Rows.Count; x++)
{
GridViewRow row = (GridViewRow)NameOfYourGrid.Rows[x];
if(row("pain1").ToString() == "23")
CheckBox chk = (CheckBox)row.Cells[0].FindControl("The_Name_Of_The_Control");
}
Or, if you wich to do it on the binding ;
In the .aspx file put an explicit property on your check box :
<asp:CheckBox ID="chkInside" runat="server" Checked='<%# CheckForChecked(Container.DataItem as Whatever_the_object_is) %>' ></asp:CheckBox>
And link that event in the code behind :
protected bool CheckForChecked(Whatever_the_object_is OBJ)
{
try
{
return (bool) OBJ.Boolean_value;
// here you are supposed to have a compete Object
// OBJ.ID is suposed to be the rigth id
}
catch
{
return false;
}
}
Then the checkBox will be checked if the value is true on the gridview binding. You won't need the ID if your binding is done correctly.
I took j-f's response and got it to work when place in the right spot. At the end of filling the dataset I added this code:
Dim row As GridViewRow
For Each row In gv_pain.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim lblDrugID As Label = row.FindControl("lbl_DrugID")
If lblDrugID.Text = dt.Rows(0)("pain1").ToString() Then
Dim cbPain As CheckBox = row.FindControl("CheckBoxPain")
cbPain.Checked = True
End If
End If
Next
This goes through my rows and check the correct ones.
I have two email fields, one a text field (index 15), the other a mailto: hyperlink (index 16), both in a gridview. (And yes, I know identifying via index isn't the best way to go -- just trying to make it work at this point).
When not editing, I need to show only the hyperlink field (making it available for the user to click on). When editing, I need to show only the text field, so they can modify the value.
I've got everything working as needed except that both fields display when the grid is initially shown. If I try to hide the text field in any of the normal ways (hiding cells on RowDataBound or hiding the column upon declaration), then it doesn't show up when editing.
Here's what I'm doing so far. The RowEditing event has the following code:
GridView1.Columns(16).Visible = False
GridView1.Columns(15).Visible = True
The RowCancelingEdit event has the opposite logic, toggling visibility on both fields. And finally the RowUpdating event has the following, which turns the hyperlink display back on:
GridView1.Columns(16).Visible = True
I'm relatively new to ASP.NET, so I definitely don't know all of the constructs available.
How can I hide the text field upon normal grid display, but still have the field available to show when in edit mode?
Try to RowCommand Event and set Edit button CommandName="name"
If e.CommandName = "name" Then
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lblwwwhid = CType(row.FindControl("txtwwwhid"), Label)
lblwwwhid .visible =false
End if
It dawned on me that I could simply show/hide columns upon the initial databind (which works), as such:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DataBind()
GridView1.Columns(16).Visible = True
GridView1.Columns(15).Visible = False
End If
End Sub
I have four textboxes within an UpdatePanel, and AutoPostBack is set to True for all of them. When the user enters a value and hits Enter, I want the cursor to move to the next textbox automatically. When I don't have the controls in an UpdatePanel, the standard textbox.focus method works fine.
I found some code here that led me to create this:
Protected Sub txtField_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtField1.TextChanged,
txtField2.TextChanged, txtField3.TextChanged
'Based on the textbox where data was changed, move the cursor to the next field.
Try
Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
'As multiple textboxes fire this same event, determine which textbox triggered this.
Dim MyTextbox As TextBox = TryCast(sender, TextBox)
Select Case MyTextbox.ID.ToString
Case "txtField1"
sm.SetFocus(txtField2)
Case "txtField2"
sm.SetFocus(txtField3)
Case "txtField3"
sm.SetFocus(txtField4)
End Select
Catch ex As Exception
lblError.Text = "Error in [txtField_TextChanged]: " & ex.Message
End Try
End Sub
What is really strange is that this works ONCE on whatever field I try first. After that, the event is fired, but the focus does not change. Is there something additional I need to to for subsequent calls?
I would appreciate any suggestions. Thank you!
Try setting the EnablePartialRendering property of the ScriptManager to false, like so:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false"></asp:ScriptManager>
I hope this helps! Good luck, and happy coding :)
Is it possible to disable CompareValidator / RequiredFieldValidator from code behind in VB.net?
I have 3 field (dropdown and two dated picker) and once the month from dropdown is selected I want to disable validation / required on datepicker inputs.
Any suggestions much appreciated!
Please note have already tried this, and it does not work:
Protected Sub DashbodropardMonthsDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DashbodropardMonthsDropDown.SelectedIndexChanged
' If Month is selected from DD menu
Dim MonthDD As Integer = DashbodropardMonthsDropDown.SelectedValue
' If nothing selected or selected "--select" with value 0
If MonthDD = 0 Then
'Validate DatePicker
CompareValidatorT1.Enabled = True
RequiredFieldValidator2.Enabled = True
RequiredFieldValidator1.Enabled = True
Else
'Do Not Validate DatePicker
CompareValidatorT1.Enabled = False
RequiredFieldValidator2.Enabled = False
RequiredFieldValidator1.Enabled = False
End If
End Sub
You can enable or disable your Validators from code behind by doing something like this
CompValidator.Enabled=false
EDIT:
Setting AutoPostBack=true for the DropDownList helped
I want if checkbox1 in gridview is checked then the label1 text in gridview is Block if checkbox1 in gridview is unchecked then label1 text n gridview is unblock ...
i want to do this because is want .... to block unblock user in ASPNETDB.MDF membership table. ..
whatz wrong in this code :
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim linkbutton1 As LinkButton = Me.GridView1.SelectedRow.FindControl("LinkButton1")
Dim chk As CheckBox = Me.GridView1.SelectedRow.FindControl("CheckBox1")
If chk.Checked = True Then
linkbutton1.Text = "Block"
Dim user As MembershipUser = Membership.GetUser(GridView1.SelectedRow.Cells(1).Text.ToString)
'To block a specific user:
user.IsApproved = False
Membership.UpdateUser(user)
Else
linkbutton1.Text = "UnBlock"
Dim user As MembershipUser = Membership.GetUser(GridView1.SelectedRow.Cells(1).Text.ToString)
'To block a specific user:
user.IsApproved = True
Membership.UpdateUser(user)
End If
End Sub
Check in the backend if its actually updating the user. If not, then there are bigger issues here. How are you also binding the grid, every page load, or just on the initial page load and during any updates?
Does the text get changed the first time it posts back, but reverts on future postbacks? It may also be the text switch from block to unblock is not be something that automatically saves to viewstate. Therefore, you may need to reassign whether its blocked or unblocked to the linkbutton text on every postback, in RowCreated...
Could you provide some more information about what isn't working?
HTH.