Gridview Hidden Field,How to get hidden field value in gridview,DataKeys

In this article I will share how to retrieve hidden value from GridView Column.
I had a assignment I need to show only description and not ID.
Following are the steps taken to get it done.

By using Datakeynames property you will get the selected row primary key.

[CODE]

<asp:GridView DataKeyNames=”ID ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” OnRowCommand=”GridView1_RowCommand” OnRowDataBound=”GridView1_RowDataBound” OnRowDeleted=”GridView1_RowDeleted” OnRowDeleting=”GridView1_RowDeleting”>

<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:Label id=”lblSelect” runat=”server” Text=’<%# Eval(“Description”) %> />
<asp:HiddenField ID=”hdID” runat=”server” Value=’<%# Eval(“ID”) %> />
</ItemTemplate>
</asp:TemplateField>

[CODE BEHIND]

To get ID for selected row.

If you are using RowUpdating, rowdatabound
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

Please visit my official blog for future article

http://blog.joggee.com

 

 

GridView- Difference between asp:hyperlinkfield and asp:hyperlink

<asp:gridview id=”titlesGrid” runat=”server”
      datasourceid=”titles”
      width=90% cellpadding=5 font-size=”8pt”
      autogeneratecolumns=false
      headerstyle-backcolor=”maroon”
      headerstyle-forecolor=”khaki”
      headerstyle-font-bold
      rowstyle-verticalalign=”top”>

      <columns>
         <asp:hyperlinkfieldheadertext=”Title”
            datatextfield=”title”
            datanavigateurlformatstring=”page_details.aspx?id={0}”
            datanavigateurlfields=”id” />

          <asp:boundfield headertext=”ID”
            datafield=”id” />

         <asp:boundfield headertext=”Name”
            datafield=”Name” />

         <asp:boundfield headertext=”Fee”
            datafield=”fee”
            htmlencode=false
            dataformatstring=”{0:n2}”
            itemstyle-horizontalalign=”right” />

      </columns>

   </asp:gridview>

The only drawback I found in a asp:hyperlinkfieldthat you cannot bound the field or concatenate the string like below expression. You will get an error. 

<asp:hyperlinkfieldheadertext=”Title”
            datatextfield=”title”
            datanavigateurlformatstring=”page_details.aspx?id={0}
&status=<%= Status %>”  datanavigateurlfields=”id” />

Below is a suggested way to get it done.
Create an template column and add a hyperlink to it.

<asp:TemplateField HeaderText=”Link”>
<ItemTemplate>
<asp:HyperLink ID=”hltitle” runat=”server” NavigateUrl='<%# FormatUrl( (int) Eval(“title”) ) %>’ Text = ‘<%# Eval(“title”) %>‘ />

</ItemTemplate>
</asp:TemplateField>

Most recommendable way to do it, do it through code behind. use RowDataBound events.

Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If
e.Row.RowType = DataControlRowType.DataRow Then
‘You can bound your data here 
End If

I would appreciate your comments.
Rana
 

301 Redirect

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It’s not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it’s the safest option. The code “301” is interpreted as “moved permanently”.

Below are a Couple of methods to implement URL Redirection

IIS Redirect

  • In internet services manager, right click on the file or folder you wish to redirect
  • Select the radio titled “a redirection to a URL”.
  • Enter the redirection page
  • Check “The exact url entered above” and the “A permanent redirection for this resource”
  • Click on ‘Apply’

ASP .NET Redirect

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.new-url.com&#8221;);
}
</script>

Redirect Old domain to New domain (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Please REPLACE http://www.newdomain.com in the above code with your actual domain name.

In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Project Workflows and Roles

ASP.NET TODO Comments

Using TODO, HACK, and UNDONE Comments
TODO comments is really useful command. When you begin a comment with ToDo or TODO or however you want to capitalize it, you can then see this comment in your task list by changing the task list options. This is very useful for marking parts of your code that you later need to work on. By default, there is also a HACK and UNDONE comment that I use from time to time. You can add comments that can be seen on the task list by going to Tools->Options, then under the environment folder select Task List.

‘Todo: Missing parameters need to be pass.