I'm new to all of this code, so please be patient.
On my gridview, I am trying to make it so, once I check on of the checkboxes, the others get greyed out, making it impossible to select another. How would I code this?
Thanks in advance.
PS: I'm using vb, rather than C#.
Edit: Here is the code I want to run.
Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles btnOK.Click
Dim TxtbxID As TextBox
Dim RB As System.Web.UI.WebControls.RadioButton
For Each row As GridViewRow In GridView1.Rows
RB = row.FindControl("select")
If RB.Checked Then
Me.txtHiddenProblem.Text = row.Cells(3).Text & " on " & row.Cells(5).Text
TxtbxID = FormView1.FindControl("ProblemTextbox")
TxtbxID.Text = txtHiddenProblem.Text
End Sub
Take a look at this article: http://www.ezzylearning.com/tutorial.aspx?tid=5187857
It has some lines of code which help you determine the particular gridview row the checkbox element was associated with...
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
//other code
}
Now all we need to do is when the checkbox element is checked, iterate through the grid, find the checkbox, and disable them if its not the same as the one which caused the event...
Related
I am trying to change a cell value in a GridView when a user edits another cell value in that GridView. Here is the RowUpdating function that I have tried:
Private Sub Gv_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles Gv.RowUpdating
Dim Gvr As GridViewRow = DirectCast(sender.NamingContainer, GridViewRow)
Dim lessonName As String = Gvr.Cells(2).Text
Gvr.Cells(3).Text = GetLessonID(lessonName)
End Sub
As you can see, I tried casting the naming container of the sender to a GridViewRow, but that doesn't work. I thought the naming container would be the GridView itself, but it is not. Basically, I need to know how to access the cell that needs to be automatically changed in the RowUpdating function. I should mention that the cell in question cannot be edited by the user; rather, it is updated automatically when the Update button is clicked. Any help is greatly appreciated. Please let me know if I can clarify anything. Thanks!
Leverage the RowIndex Property from the GridViewUpdateEventArgs parameter and access the grid directly:
gv.Rows(e.RowIndex).Cells(3).Text = grv.Rows(e.RowIndex).Cells(2).Text
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 an asp.net page written with vb.net that has a gridview table. The table is setup to have a checkbox column for each row. If this checkbox is checked for a row, I do some additional things. One of the other columns also includes a hyperlink on the text for each cell in that column.
I have a button on the page that will loop through and programmatically check each one of the checkboxes. The button does this correctly; however, it also removes the hyperlink from the other column for some reason. Any ideas why?
Here is the code where I set the hyperlinks for the hyperlink column:
Protected Sub gridData_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridData.RowDataBound
'When a row is added to the data grid view we need to add the hyperlink to the zipname column
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cell As TableCell = e.Row.Cells(4)
Dim lnk As HyperLink = New HyperLink
'Set properties of the link
lnk.NavigateUrl = cell.Text
lnk.Text = cell.Text
'Format the filename, filepath, zipname, and username to be lowercase
e.Row.Cells(1).Text = LCase(e.Row.Cells(1).Text)
e.Row.Cells(3).Text = LCase(e.Row.Cells(3).Text)
e.Row.Cells(4).Text = LCase(e.Row.Cells(4).Text)
e.Row.Cells(5).Text = LCase(e.Row.Cells(5).Text)
'Add the hyperlink
cell.Controls.Clear()
cell.Controls.Add(lnk)
End If
End Sub
Here is the code for the check all button:
Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click
'Check all of the download checkboxes
For Each row As GridViewRow In gridData.Rows
CType(row.FindControl("CheckBox1"), CheckBox).Checked = True
Next
End Sub
As is the usual case with Dynamic Controls, All the dynamically created Hyperlinks in RowDataBound event needs to be recreated every postback.
So, there are 2 options.
1.) Either bind your GridView on every postback :
protected void Page_Load(object sender, EventArgs e)
{
gridData.DataBind();
}
OR
2.) Turn off ViewState for your GridView::
<asp:GridView ID="gridData" EnableViewState="false" ... />.
On using any of above options, the OnRowDataBound event is executed each time postback happens, thus recreating the Dynamic controls. Now you can access the Hyperlinks in your button click event.
Check properly which one of above options is best for your application.
i created a textbox, a button and a panel in a page. So my aim is to create links which will be put inside a panel. However, it turns out that every time i add another one, it seems to replace the previously created link. Is there a way to just add the links, not replacing previously created links? I really don't have a background that much in ASP.
This is the code I researched.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim link As New HyperLink()
Dim ltr As New Literal()
link.Text = TextBox1.Text
link.NavigateUrl = "Default.aspx?field1=" + TextBox2.Text + " "
ltr.Text = "<br/>"
Panel1.Controls.Add(ltr)
Panel1.Controls.Add(link)
Please help me. Thanks!
As said by #Rex, when post back occurs dynamically controls will be removed from the page until we retain them in Init Event.
While adding controls to PlaceHolder, save the same data like ID, Text..etc. in a object like session variable and retrieve the same data in Page Init event and add to the place holder.
protected void Page_Init(object sender, EventArgs e)
{
// Retrieve from session variable and add the same data to the place holder.
}
I have gridview bound to an Entity Data source which works no problem, however when I try to programatically change a header columns text, it appears to break the styling and will not allow sorting either, below is how I am trapping and changing the Header row column text.
Does anyone have any ideas?
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
'retrieve the values from the userdeftable
e.Row.Cells(6).Text = App.Session.Company.UserDef3
End If
End Sub
Use the HeaderText-property of the column.
Me.gv1.Columns(6).HeaderText = App.Session.Company.UserDef3
Use the Sorted event...
Example of how to toggle HeaderText to show the order of the data.
protected void gvCurrCheckIns_Sorted(object sender, EventArgs e)
{
if (gvCurrCheckIns.Columns[8].HeaderText.Contains("(DESC)"))
gvCurrCheckIns.Columns[8].HeaderText = "Checked IN (ASC)";
else
gvCurrCheckIns.Columns[8].HeaderText = "Checked IN (DESC)";
}