Click to See Complete Forum and Search --> : geting hidden field value in .ascx.cs file


@kriti
December 31st, 2009, 02:43 AM
Hey m not able to retrieve the hidden fileld(hiddenfield) value in Program.ascx.cs from Program.ascx file

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Program.ascx.cs" Inherits="a.b.c.Program" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<%@ Import namespace="System.Text" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id ="Head1" runat="server">
<script type="text/javascript">
function ConfirmIt() {
var x = confirm("Do you Want to notify changes to User ??");
var control = '<%=hiddenfield.ClientID%>';
if (x == true) {
document.getElementById(control).value = "1";
}
else {
document.getElementById(control).value = "0";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<SCRIPT language="javascript" src="js/calendar.js">

</SCRIPT>
<script event="onbeforeunload" for="window">
showWaitMessage();
</script>

<TABLE style="FONT-SIZE: xx-small; FONT-FAMILY: verdana" cellPadding="2">
<TR>
<TD>
<asp:label id="StartDate" Font-Size="XX-Small" Font-Names="Verdana" Text="Add "
Runat="server" Font-Name="Verdana">Start Date (mm/dd/yy):</asp:label></TD>
<TD>
<asp:TextBox style="TEXT-ALIGN: center" Wrap="False" MaxLength="10" id="StartDate" Height="18px"
runat="server" Width="75"></asp:TextBox></TD>
<td>&nbsp;&nbsp;</td>
<td>
<asp:label id="EndDate" Font-Size="XX-Small" Font-Names="Verdana" Text="Add: "
Runat="server" Font-Name="Verdana">End Date (mm/dd/yy):</asp:label>
</td>
<td>
<asp:TextBox id="EndDate" runat="server" Height="18px" Wrap="False" MaxLength="10" Style="TEXT-ALIGN:center"
Width="75"></asp:TextBox>
</td>
</TR>
</TABLE>


<table>

<td colSpan="3">

<asp:datagrid id="m_grid" Width="1077px" runat="server" HorizontalAlign="Center" BackColor="White"
Font-Names="Verdana" BorderStyle="None" BorderWidth="2px" BorderColor="Gray" CellPadding="3"
PageSize="20" AllowSorting="True" AutoGenerateColumns="False">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#FFFF80"></SelectedItemStyle>
<AlternatingItemStyle BackColor="LightCyan"></AlternatingItemStyle>
<ItemStyle Font-Size="XX-Small" Font-Names="Verdana" ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Size="XX-Small" Font-Names="Verdana" Font-Bold="True" ForeColor="White" BorderStyle="Inset"
BorderColor="Silver" BackColor="SteelBlue"></HeaderStyle>
<Columns>
<asp:HyperLinkColumn Target="_blank" DataNavigateUrlField="Link" DataTextField="Number" SortExpression="Number"
HeaderText="Number">
<HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:HyperLinkColumn>

<asp:BoundColumn Visible="False" DataField="Pre" SortExpression="Pre"></asp:BoundColumn>

<asp:TemplateColumn SortExpression="Pre" HeaderText="Pre?">
<HeaderStyle HorizontalAlign="Center" Width="80px"></HeaderStyle>


<ItemTemplate>
<div>
<input id="inpHide" type="hidden" runat="server" />
<asp:DropDownList id="dd" runat="server" Width="87px" Font-Size="XX-Small" Font-Names="Verdana" Font-Bold="True" ForeColor="Black" onselectedindexchanged="dd_SelectedIndexChanged" onChange="ConfirmIt()" AutoPostBack="true" >
<asp:ListItem Value="Pending">Pending</asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
</div>
</ItemTemplate>


</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="Notes" SortExpression="Notes"></asp:BoundColumn>
<asp:TemplateColumn SortExpression="Notes" HeaderText="Pre_Notes">
<HeaderStyle Wrap="True" HorizontalAlign="Center" Width="100px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:TextBox id="txtNote" TextMode="MultiLine" wrap="true" Columns="30" Rows="2" runat="server" Font-Names="Verdana" Font-Size="XX-Small"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="State" SortExpression="State" ReadOnly="True" HeaderText="State">
<HeaderStyle HorizontalAlign="Center" Width="100px"></HeaderStyle>
</asp:BoundColumn>


</tr>
</table>
</form>

<SCRIPT language="javascript">


function showWaitMessage()
{

}

//many more functions.

</SCRIPT>


</body>
</html>

My Program.ascx.cs is

namespace a.b.c
{
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web;



/// <summary>
///
/// </summary>
public partial class Program : System.Web.UI.UserControl

{
private string tDate = string.Empty;

#region Class Variables
//strings and variables initialization
#region GUI Components
//all gui componenets..
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.ImageButton btnRefresh;

#endregion
#endregion

#region Public Properties
public bool IsAdmin
{
}

public string SortExpression
{

}

//many public functions

#endregion

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSaveChanges.Click += new System.Web.UI.ImageClickEventHandler(this.btnSaveChanges_Click);
this.btnExcel.Click += new System.Web.UI.ImageClickEventHandler(this.btnExcel_Click);
//many more
this.Load += new System.EventHandler(this.Page_Load);
// this.ddPreApproved.SelectedIndexChanged += new System.EventHandler(this.ddPreApproved_SelectedIndexChanged);
}
#endregion
#region Public Methods
//many more public method..
public void Refresh()
{
try
{

}
catch (Exception ex)
{

}
}
#endregion
#region Private Methods

//many private functions...i have just displayed mine..I have changed private to //protected..then also its not working
private void ddPreApproved_SelectedIndexChanged(object sender, EventArgs e)
{
int DiagResult = int.Parse(inpHide.Value);

if (DiagResult == 1)
{
//Do Somthing
Response.Write("You Have Selected Ok");
}
else if (DiagResult == 0)
{
//Do somthing
Response.Write("You have Selected Cancel");
}


}
private void btnSaveChanges_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{

}
#endregion
}
}

Can u think of any soln for this so that hiddenfield is retrieved..Do u think there is problem in the Program.ascx..Just check it if i have put form and div correctly... And also i have not pasted all my code but only the code that is neccessary...Here the program which i am doing goes like this..If user does any changes in the drop-downlist,It should notify to the user..For that m using hiddenfield concept but its not working ..

Alsvha
January 4th, 2010, 07:29 AM
First: You should only post the relevant code and not entire pages as few will look at those pages.
It will also perhaps help you isolate the problem and solve it for you.

Second: You should use code tags to format the code a bit nicer, making it easier to read.

As for your actual problem, my guess is that you try to access a control before it is populated or similar, and thus you face the life-cycle model of when events gets triggered and when you have access to viewstate or if you've build dynamic controls.
But it is difficult to deduct because your code is so unreadable due to the amount of code and lack of code tags.

There's normally no problem getting the value, unless you dynamical create the control, use it through a repeater and don't access it correctly and so on. (The repeater likely is your problem)