Tuesday, March 25, 2008

modifying an xml file

To read the xml file :

XmlDocument xd = new XmlDocument();
xd.Load(filename);

To save the file:

xd.Save(filename);

but you'll have issues saying File is being used by other process - "read more about this in the link below"; 

So, create a filestream to be used. 

XmlDocument xd = new XmlDocument();

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

xd.Load(fs);

fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);

xd.Save(fs);

Fore more detailed explanation check this link: 

http://geekswithblogs.net/chrisfalter/archive/2006/03/27/73515.aspx

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