Losing custom gridview font when new page selected - asp.net

I am using this code to change the font color in a grid view for a single column depending on value:
For Each row As GridViewRow In gvSearch.Rows
If row.Cells(8).Text.Trim = "Used" Then
row.Cells(8).CssClass = "CautionRow"
End If
Next
This code runs after the gridview databind. However, the gridview has pages available and this code only changes the first page of the grid view. I can resolve the problem by not allowing pages, but this is a tacky solution. Any suggetions?

Register PageIndexChanging event
onpageindexchanging="gvSearch_PageIndexChanging"
Then in event handler do your font changing logic like
Sub gvSearch_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
{
For Each row As GridViewRow In gvSearch.Rows
If row.Cells(8).Text.Trim = "Used" Then
row.Cells(8).CssClass = "CautionRow"
End If
Next
}

Actually found my own answer thanks to help from my fellow programmer. Here's what really works:
In the style sheet (.css) add this:
.CautionRow {
color: red;
}
... then add this to your code:
Protected Sub gvSearch_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViwRowEventArgs) Handles gvSearch.RowDatabound
If e.Row.Cells.Count > 1 Then
If e.Row.Cells(8).Text.ToString.ToLower.Trim = "used" Then
e.Row.Cells(8).CssClass = "CautionRow"
End If
End If
End Sub

Related

Rise event from user control written in main page

I have searched this site for what I need, but none of the answers just quite fits my needs. So here's the deal:
I am dynamically loading a usercontrol to aspx page along with some buttons like this:
Dim uc As UserControl
Dim btns As New List(Of LinkButton)
uc = LoadControl("path_to_ascx")
btns.Add(New LinkButton With {.ID = "btnid", .Text = "Sometext"})
uc.GetType().GetProperty("tblButtons").SetValue(uc, btns, Nothing)
holder2.Controls.Add(uc) 'holder2 is an id of a PlaceHolder
An this work perfectly fine. The buttons show on the page as expected. Now I am having trouble, how to tell these buttons to rise an event written in aspx page, to which a usercontrol is being loaded to.
Public Sub btnClick(sender As Object, e As EventArgs)
'do stuff
End Sub
Why I want to achieve this? Because I have a pretty complex UserControl which I want to reuse as much as possible, but buttons will do different stuff on every aspx page this UserControl will be loaded to.
Thanks
I have solved my problem. In UserControl, I added dynamic buttons to every row on OnRowCreated event.
I was loading UserControl to aspx with buttons like in my question, I just added an ID property to my usercontrol:
Dim uc As UserControl
Dim btns As New List(Of LinkButton)
uc = LoadControl("path_to_ascx")
btns.Add(New LinkButton With {.ID = "btnid", .Text = "Sometext", .CommandName = "cmdName"})
uc.ID = "UserControl1" 'here I added ID property
uc.GetType().GetProperty("tblButtons").SetValue(uc, btns, Nothing)
holder2.Controls.Add(uc) 'holder2 is an id of a PlaceHolder
And after I add an EventHanlder like this:
AddHandler TryCast(holder2.FindControl("UserControl1").FindControl("grid"), GridView).RowCommand, AddressOf grid_RowCommand
'grid is the ID of GrdiView in UserControl
And here is the event for gridview rowCommand written in aspx code behind:
Protected Sub grid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "someCmdName" Then
'do stuff
Else
'do somthing else
End If
End Sub
Maybe my question was not good enough, because I did not mention, that I will be loading buttons to gridview row on rowCreated event and hook them up to RowCommand for wich I apologise.
If someone knows another way to do this, it would be much appreciated to share.
Regards

wont add updated check boxes from gridview to arraylist

On my aspx page I have a gridview template that when it is rendered has 100 - 200 rows and each row has a check box.
When the page loads 5 check boxes are automatically checked. If I manually check another 3 then press the submit button, then loop through all the gridview rows to find out which rows had a checked box then it still remembers the original 5. How do I make it remember the updated 8?
Protected Sub mySubmitButton_Click(sender As Object, e As EventArgs)
Dim myArray As ArrayList = New ArrayList()
For Each myRow As GridViewRow In MyGridview.Rows
If CType(myRow.FindControl("MyCheckbox"), CheckBox).Checked Then
myArray.Add(MyGridview.DataKeys(MyRow.RowIndex).Value)
End If
Next
'add to DB myArray
End Sub
I tried this on the gridview in the asp.net and it made no difference
ViewStateMode="Disabled" and ViewStateMode="Enabled"
Thanks for the help
Is the ViewState enabled on the GridView?
Have you got the logic to load your Grid in the Page_Load event, if so add it in if(!IsPostBack)

Using a checkbox outside of a datagrid to affect its contents

I am using a Checkbox outside of a datagrid. When i select the check box autopostback is true, and this would then show the image, but i cant access the images within the datagrid with that script. If i use a seperate image outside of the datagrid the script works. How can i get this script to work finding when the checkbox out side of the datagrid is checked to then show the image within the datagrid?
The script i am using is
<script runat="server">
Sub Check(sender As Object, e As EventArgs)
If checkShowImages.Checked Then
img.Visible = True
Else
img.Visible = False
End If
End Sub
</script>
Try this(Assuming your checkbox's id is "CheckBox1" & DataGrid's Id is "Datagrid1")...
Sub Check(sender As Object, e As EventArgs)
For Each r As DataGridItem In Datagrid1.Items
Try
r.FindControl("img").Visible = CheckBox1.Checked
Catch ex As Exception
End Try
Next
end sub
Hope this helps. Good luck.
If I understand correctly, you have images in your GridView that you want to display when a checkbox outside is selected. To do this, you will need to iterate through the rows of your GridView like so:
foreach (GridViewRow row in myGrid.Rows)
{
Image myImage = row.FindControl("HiddenImage") as Image;
//Hide or show image based on checkbox state
myImage.Visible = checkShowImages.Checked;
}
VB (a little rusty):
For Each row As GridViewRow In myGrid.Rows
Dim myImage As Image = TryCast(row.FindControl("HiddenImage"), Image)
'Hide or show image based on checkbox state
myImage.Visible = checkShowImages.Checked
Next

Updating button text when FormView item is Updated

I'm still somewhat new to ASP, and can't seem to figure out what the issue is.
When I update an item in FormView, I need the text of a button to change.
I'm using:
Protected Sub fvClientInfo_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fvClientInfo.ItemUpdated
btnEditClient.Text = "Edit"
End Sub
The line of code is being called (can tell from debug mode), but the button text isn't changing and I have no idea why.
I should make sure to mention that the button is NOT inside the FormView.
Any ideas?
Try this.
Protected Sub btnchange() Handles FormView1.DataBound
Button1.Text = "Newtxt"
End Sub
and add this to your formview
OnDataBound="btnchange"

asp.net masterpage - make hyperlink visible at runtime

Is it possible to alter the visible property of hyperlinks on a masterpage at runtime?
thanks
If you expose the hyperlink as a property on the master page, you can get a reference to a pages master and cast that to your specific master page then set visibility of the hyperlinks using that property.
In your master page have something like this:
Public ReadOnly Property RemitViewerLink() As HyperLink
Get
Return hlRemitViewer
End Get
End Property
Then in your child page you can do this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim MyMaster As MasterPage = DirectCast(Page.Master, MasterPage)
MyMaster.RemitViewerLink.CssClass = "selectedMenuItem" 'or set visibility
End Sub
No, if you set visible=False then it won't even display in the HTML output of the page. You would need to use javascript to hide/show the hyperlinks instead.

Resources