Set Value of HiddenField in GridView to new GUID - c#

I have a GridView control with an ItemTemplate that has a HiddenField.
<asp:GridView ID="GridView1" runat="server" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value='<% Response.Write(Guid.NewGuid()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The problem is that it actually renders exactly <% Response.Write(Guid.NewGuid()) %> instead of a Guid. How do I set the value to render as a Guid and execute the code rather than interpret it as a literal. I've tried using both single quotes ' and double quotes ".

You can try
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value='<%# Guid.NewGuid().ToString() %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to add # In inline coding . Ex : '<%# //code here %>' and
you are setting HiddenField1 value ,so you dont need to use Response.Write . The value should be Value='<%# Guid.NewGuid().ToString() %>' . Hope this will do

You can get the GUID in code behind as follows.
HiddenField HiddenField1 = (HiddenField)row.FindControl("HiddenField1");
Guid guid1;
Guid1.TryParse(HiddenField1.Value, out guid1);
To set the value, you can use
HiddenField1.Value = guid1.ToString();

Related

how can i make a gridview rows clickable

i have a gridview that i filled it with filenames from a path, know, i want the filenames to be clickable so i can download them and i don't know how, can someone help me.
code-behind (file is the gridview ID)
string path = "//the path";
string[] Fname = Directory.GetFiles(path).Select(Path.GetFileName).ToArray();
file.DataSource = Fname;
file.DataBind();
the web page
<asp:GridView ID="file" runat="server" style="border:hidden" ></asp:GridView>
You can bind a single list/array item and get it in the ItemTemplate like this:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a target="_blank" href="<%# Container.DataItem %>">
<%# Container.DataItem %>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink ID="UserId" runat="server" DataNavigateUrlFields="UserId" NavigateUrl='<%# "~/Report.aspx?name=" %>'
Text='<%# Eval("UserId") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Accessing Header Text property of GridView TemplateField in the Code Behind when it is set from Header Template

I have a GridView in an asp .net application, where the Header Text of a template field is set in the Header Template of the field, as a label (where it will come from the resource file). Below is the code
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<HeaderTemplate>
<asp:Label ID="lblNameHeader" Text="Name" runat="server" meta:resourcekey="lblNameHeaderResource1"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>'/>
</ItemTemplate>
</Columns>
</asp:GridView>
In code behind I am trying to access the HeaderText set on the column like this
var headerText = gridView.Columns[0].HeaderText;
But the value is coming Empty and I am not able to retrieve it from the gridView.Columns' HeaderTemplate property as well.
Please help me.
You have several problems with your code.
You are missing a closing </asp:TemplateField>. Your asp:Label is missing a runat="server" attribute.
If you want to use the .HeaderText property, this should be your markup:
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If you want to use a HeaderTemplate with custom markup in it, then you need to cast the column to a TemplateField, then access the controls within it.

Click event for ItemTemplate inside asp:GridView

I have a Gridview with the following markup:
<asp:GridView ID="gdvResxKeyValue" runat="server" Width="100%" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I need to have a handler for the Image click event. Is there any easier way to do so?
You can use an Image Button instead of Image. Try this code.
<asp:GridView ID="gdvResxKeyValue" runat="server" Width="100%" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" OnClick="YourEventName" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You just need to specify your server side event name here.
Use an asp button and set its style to be display:none
<asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" onclick="ClickImage(this)" />
.......
<asp:Button ID="hiddenButton" runat="server" OnClick="hiddenButton_Click" style="display:none"></asp:Button>
<script type="text/javascript">
function ClickImage(imageControl)
{
document.getElementById('<%=hiddenButton.ClientID%>').click();
}
</script>
This will raise the server side event of the button and you can do your work there.
Try this:
jquery Solution
<asp:Image ID="imgEditResxValue" CssClass="sfEdit" runat="server" ImageUrl="~/Administrator/Templates/Default/images/imgedit.png" onclick="this.next().click()"/>
<asp:LinkButton Text="text" runat="server" OnClick="call_method"/>

Bind only value from KeyValuePair to GridView

In ASPX File
<asp:GridView ID="gvSavedAddresses" runat="server">
<Columns>
<asp:TemplateField SortExpression="AddressType" ItemStyle-Width="9%" HeaderText="Type"
HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:Label runat="server" ID="lblAddressType" Text='<%#Eval("AddressType")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In CS File
objAddr.AddressType = new KeyValuePair<string, string>(dr["AddressTypeLookupID"].ToString(), dr["AddressType"].ToString());
I want to display only Value in the grid. But in my code it is showing both key and value in grid cell. how to avoid this?
Cast the field to its actual type, and retrieve Value:
<%# ((KeyValuePair<string, string>)Eval("AddressType")).Value %>

How To change Command Text and ImageButton of ItemTemplate in TemplateField

I have a column that has an ImageButton. my database field has bit data type. I want when my record has true value in that column show True.jpg and my command become MakeFalse and when it has false value show False.jpg and my command become MakeTrue. How I can do this?Is it possible to do it with one TemplateField?
thanks
You could include two ImageButtons in a TemplateField and evaluate Visible from your bit_field
<asp:TemplateField HeaderText="YourField">
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="True.jpg" Visible='<%# (bool)Eval("bit_field") %>' />
<asp:ImageButton runat="server" ImageUrl="False.jpg" Visible='<%# !(bool)Eval("bit_field") %>' />
</ItemTemplate>
</asp:TemplateField>
I'm not sure how you'd want your Command to tie in.
This is the remaining part of the above Brissles code
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text='<%#(bool)Eval("bit_field")? "Make False":"Make True" %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources