Wednesday, July 25, 2007

How to access the variable of SourceView (.aspx) in CodeBehind page (.aspx.cs)

If you want to access the value of a variable in the code behind page (.aspx.cs), which is declared in the .aspx. You can do it this way:

Eg.: The following code in .aspx page has a Javascript function which has a variable declared. The variable is used to store the value returned as a result of the user clicking the "Confirm Box" on client side. So, here the function gets called on the "OnClientClick" event. This event is one of the useful additions to asp.net.
Here, the "CreateNewEvent()" function gets called on "OnClientClick" event

==============================================================
head runat="server"
titleUntitled Page /title
script language="javascript" type ="text/javascript"
function CreateNewEvent()
{
var newHdfield = document.createElement("INPUT");
newHdfield.setAttribute('id','pageTitle');
newHdfield.setAttribute('name','pageTitle');
newHdfield.setAttribute('type','hidden');
newHdfield.value = confirm("do u wanna do it ?");
document.getElementById("form1").appendChild(newHdfield,form1);
}
/script
/head
body
form id="form1" runat="server"
asp:Button ID="Button1" OnClientClick="CreateNewEvent()" runat="server" Style="z-index: 100; left: 40px; position: absolute;
top: 109px" Text="Button" /
/form
/body
================================================
.aspx.vb page might look like this:
--//On button click, in the server side event, we are just capturing the value of the variable defined in the Javascript function in the .aspx page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As String = Request.Form("pageTitle")
End Sub
================================================

So, now use the value of 'a' for our purposes in the code-behind page.

Note 1: as always, the "<" and ">" tags for the html code in the .aspx page have been removed to render the content in the blog. Please put them at the appropriate positions.

Note 2: I had to struggle for long for not making the variable (hidden field) as a child node to the Form. Because when we try to access the variable in the code-behind page using Form(integer or string value), it shows only the children of Form element in the array. So, DO NOT FORGET to make the variable (field) as a child to the FORM element.

Note 3: I did not encounter it but read on a different blog that both the 'id' and 'name' attributes should be set and they should be same. --- Try it. I do not guarantee anything from my side. I just followed their instruction.

Related Link: http://forums.asp.net/p/1057915/1511865.aspx

Thursday, July 12, 2007

How to hide GridView Column from the user

How to hide a column from user (UI) in a GridView (ASP.NET)

Solutions:

1) Set the 'visible' property for the column to 'False' in the GridView. -- This is OK if you dont want to use the column in code because this option does not render the Column at all on the UI. So, you cannot access it.

2) Use the CSS style to hide the column on UI. This option renders the column on UI but hides it. So, if you want to access it in code, you can do it.

How to apply (method 2):

a) make a css class with style 'display:none'

. HiddenCol
{
display:none;
}

b) apply this css to your column in the grid view (.aspx file)

(NOTE: Cannot include '<' '>' for tags, because the blog doesnt render them. Please use the correct tags when needed. )


columns
asp:BoundField DataField="ProductParameterUid" HeaderText="uid" ReadOnly="True"
ItemStyle CssClass="HiddenCol" /
HeaderStyle CssClass="HiddenCol" /
FooterStyle CssClass="HiddenCol" /
/asp:BoundField

/columns

NOTE: HeaderStyle and FooterStyle should be assigned same style if you dont want to display the header and footer also for that column.

Thats all and your user cannot see the column. But you can still access it. You can do this hiding in the code also. Check the following link on how to do it. http://www.netomatix.com/development/GridViewHideColumn.aspx

Hope you save some time with this.

--Shiva

Thursday, July 5, 2007

AppendDataBoundItems property of Drop Down List box

AppendDataBoundItems property of Drop Down List box

Situation: For a drop down list box (combo box), if you want to have the default item to be shown as something like "(select from the list)", you can do it easily with this "AppendDataBoundItems" property.

Procedure:
- Drag and drop the drop down list box onto the "design" part of the .aspx page
- Do the binding stuff (from a dataset or whatever) in the code behind page (.aspx.cs)
- Open the "Source" part of the .aspx page. Scroll to the drop down list box's tag. Add the following code:

NOTE: I am removing the "<" and ">" tags because they are not rendered.

"asp:DropDownList ID="regddl" runat="server" Width="480px" Font-Size="9pt" ForeColor="SteelBlue" Visible="False" AppendDataBoundItems="True">
asp:ListItem Text ="(Select a usage type)" Value = ""> /asp:ListItem>
/asp:DropDownList>"


- The "Text" property of list item is displayed in the ddl box by default. Its value is null.
- You can have a RequiredFieldValidator to validate this ddl box so as to make sure, the user has made a valid selection (neither ignored selection nor selected default comment "select from list"), like this:

"asp:RequiredFieldValidator ID="rfvUsage" runat="server" ControlToValidate="regddl"
EnableViewState="False" ErrorMessage="Usage type required." Width="164px">** Usage type required. asp:RequiredFieldValidator>"

- All the above code can save lot of server side scripting.

Additional blogs which describe the above process are:

http://weblogs.asp.net/scottgu/archive/2006/01/29/436804.aspx
http://geekswithblogs.net/chrishan/category/2134.aspx/rss

Hope this was useful.

Thanks,
Shiva


Add to Technorati Favorites

Thursday, June 21, 2007

System.ArgumentNullException: Value cannot be null. Parameter name: type at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.We

Error: System.ArgumentNullException: Value cannot be null. Parameter name: type at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Web.Profile.ProfileBase.CreateMyInstance(String username, Boolean isAuthenticated) at System.Web.Profile.ProfileBase.Create(String username, Boolean isAuthenticated) at System.Web.Profile.ProfileBase.Create(String username) at ProfileCommon.GetProfile(String username)

Core Problem:
If you have Profile Provider in your asp.net application and your deploy project's Configuration Properties, for the "Output assemblies" option has "Treat as library component(remove the App_Code.compiled file)" check box checked and you deploy it on a production machine.

Note: This runs fine on local machine.

Solution:
1) Right click on the deploy project in the solution explorer, select property pages
2) In the Configuration Properties, select Output Assemblies
3) Uncheck the "Treat as library component(remove the App_Code.compiled file)" check box.
4) rebuild and deploy.

The explanations for the problem and solutions can be found at these links:
http://forums.l-space-design.com/blogs/day_of_the_developer/archive/2006/08/12/223.aspx

http://communityserver.org/forums/p/485257/571283.aspx

http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx

Hope this saves your time. I had wasted nearly a day with this problem, using profiler to check the sql server, etc.

Good luck.

Friday, June 1, 2007

The wonders of "app_offline.htm" in ASP.NET application

app_offline.htm

If you want to kill someone trying to deploy or build or working with ASP.NET application, you can play this trick on him/her.

Steps:
1) Go to the root folder, which is the "Start up project" for their app
2) right click, select notepad and rename it as "app_offline.htm"
Thats all you are done.

When they trying to run the app, with the project (in which u created this htm file), as the startup project, it gives them an error message, which they cannot figure out unless they know about this file and look into the directory.

Pretty funny, interesting, wierd but I like it.

Friday, May 25, 2007

Error: Not associated with a trusted SQL Server connection

When (Situation): I received this error while connecting to Sql Server 2000 using Sql authentication (windows authentication works fine) from asp.net 1.1 application. The Sql Server exists on my local system and it had a database, which I was trying to access.

What (Solution):
1) Open Enterprise Manager
2) Expand Microsoft Sql Servers and then SqlServer Group
3) Right click on the server, which you are trying to access from your application, select Properties
4) Select "Security" tab, select "SQL Server and Windows" and click OK.
5) When it prompts you for restart, click OK.

Links:
http://support.microsoft.com/kb/889615