Navigation

Search

Categories

On this page

Using Javascript with ASP.NET
First Look at Silverlight 2.0
Using Findcontrol
Gridview Hidden Field, How to get hidden field value in gridview

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 103
This Year: 41
This Month: 6
This Week: 2
Comments: 0

Sign In

 Thursday, March 20, 2008
Thursday, March 20, 2008 2:53:14 PM (Eastern Standard Time, UTC-05:00) (  |  )

I've been working on a project which required the use of some javascript with some server-side controls. I spent considerable time monkeying around with the Page.ClientScript.RegisterClientScriptBlock() method which registers a client script on the page. I couldn't quite get it right using this method and ended up with a much simpler approach anyway.  Every server-side web control has an "Add" method which you can use when the page loads.   In the example below, I register a click event for a button and 2 different textboxes.  Take notice that for the textboxes I use the client side id value for them (e.g. 'Header1_txtFirstName').  These values can be obtained by looking at the source code of the page in the browser.

Private Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
        If Not Page.IsPostBack Then
            btnDelete.Attributes.Add("onClick", "return confirm('You sure you want to delete the record?');")
            txtFirstName.Attributes.Add("onClick", "document.getElementById('Header1_txtFirstName').value = '';")
            txtLastName.Attributes.Add("onClick", "document.getElementById('Header1_txtLastName').value = '';")
        End If
End Sub
Candidate Name: 
<asp:TextBox ID="txtFirstName" runat="server" Text="First Name" /> 'this becomes Header1_txtFirstName
<asp:TextBox ID="txtLastName" runat="server" Text="Last Name" /> 'this becomes Header1_txtLastName
More information on this topic here: http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx
 Tuesday, March 11, 2008
Tuesday, March 11, 2008 10:45:50 AM (Eastern Standard Time, UTC-05:00) ( )

 

Scott Gutherie has a nice 8 part tutorial series on Silverlight 2.0 as well as a nice blog post on Expression Blend with Silverlight 2.

 Wednesday, March 05, 2008
Wednesday, March 05, 2008 4:26:52 PM (Eastern Standard Time, UTC-05:00) ( )


I've been working on a project which has involved finding the values or setting the values on controls (label, dropdownlist etc.) embedded inside of other other controls (repeater, gridview etc.).  You have to drilldown or look inside the parent object containing these controls to make this work.  This is where the Page.FindControl method comes in useful. Here are some examples:

        Dim lb As LinkButton = sender
        If Not (lb Is Nothing) Then
                        
        'Get the value for a dropdownlist using Parent.FindControl
        Dim strDirRace As String = CType(lb.Parent.FindControl("ddlDirRace"), DropDownList).SelectedItem.Text

        'Get the value for a dropdownlist specifying the name of repeater control housing it
        Dim strDirRace As String = CType(rptDirectorDetails.FindControl("ddlDirRace"), DropDownList).SelectedItem.Text
            
        Dim lblDirRace As Label = CType(lb.Parent.FindControl("lblDirRaceUpdateStatus"), Label)
            lblDirRace.Text = "* Update Complete *"        
        End If
        When you use a Master page, then you need to do this        

          Dim cbx As CheckBox = CType(Master.FindControl("Content1").FindControl("cbxAllTel"), CheckBox)

More info on this topic: http://msdn2.microsoft.com/en-us/library/31hxzsdw.aspx
 
 Monday, March 03, 2008
Monday, March 03, 2008 12:44:01 PM (Eastern Standard Time, UTC-05:00) ( )

 

A GridView contains a property called "DataKeyNames" which is typically the primary key of each record.  This is often useful when you need to grab the unique recordID for each row.  To get ID for a single selected row, you can do something like this:

If you are using RowUpdating, RowDatabound, etc.

Dim intId as Integer= GridView1.DataKeys(e.RowIndex).Value

By name :

Dim intId as Integer= GridView1.DataKeys("ID").Value 

By Index:

Dim intId as Integer= GridView1.DataKeys(0).Value
<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>

To get the ID value for a group of records in a Gridview, you can do something like this:

Protected Sub btn_ExportSelectedRecords_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        'First loop through the GridView and see which IDs were selected. I use the StringBuilder since the list could be a very long list        
        Dim sb As New StringBuilder
        Dim row As GridViewRow
        For Each row In searchResultsGridView.Rows
            If (CType(row.FindControl("CheckBox1"), CheckBox)).Checked Then
                lblSelectedIDDirs.Text = sb.Append(searchResultsGridView.DataKeys(row.RowIndex).Value.ToString() + ","c).ToString()
            End If
        Next
End Sub
<asp:GridView ID="searchResultsGridView" Runat="server" DataSourceID="searchResultsDataSource" AllowSorting="true" AllowPaging="true" PageSize="20" 
 DataKeyNames="IDDir">
                       
 <Columns>
              
 <asp:TemplateField HeaderText="">
 <ItemTemplate>
                    <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("IDDir") %>' />
                </ItemTemplate>
                </asp:TemplateField> 
                
                 <asp:TemplateField HeaderText="">
                 <ItemTemplate>
                    <asp:Checkbox id="Checkbox1" runat="server" /> 
                 </ItemTemplate>
                </asp:TemplateField>