In order to display multiline text from the database, I used the following code. I used textbox and removed its borders (set BorderStyle= None and BorderWidth= 0px) and scrollbar (To hide scrollbar, added style="overflow:hidden in ASP textbox tag).
string newsBody = GetText(); // From the database
NDNewsBody_TextBox.Rows = newsBody.Length;
NDNewsBody_TextBox.Text = newsBody;
ASP.NET, C#.NET, CruiseControl.NET, Microsoft SQL Server, MS-DOS Batch, MySQL, nAnt, NodeJs, PowerShell, Python, Selenium RC, Redis, WebAii, Ubuntu
Thursday, April 03, 2008
Monday, March 31, 2008
Bound custom data to the Column of the GridView
Today I learned how to attach custom value to the column of the GridView control in ASP.NET page. What I did is as follows
First I click on Edit Templates of the GridView and inside ItemTemplate I dragged one Hyperlink control. For that Hyperlink's NavigateUrl and Text I used databindings.
Later I added new column to the GridView and selected TemplateField. Now the template field column in the GridView shows the Hyperlink whose URL and Text are bound to the custom date.
First I click on Edit Templates of the GridView and inside ItemTemplate I dragged one Hyperlink control. For that Hyperlink's NavigateUrl and Text I used databindings.
Later I added new column to the GridView and selected TemplateField. Now the template field column in the GridView shows the Hyperlink whose URL and Text are bound to the custom date.
Unique Identifier Generator in .NET
The following code generates the Unique Identifier in string format..
Guid.NewGuid().ToString();
Guid.NewGuid().ToString();
Wednesday, March 26, 2008
Get Columns Names of the MS SQL Table
Following MS SQL query gives the list of column names of the table_Name
select col.name from sysobjects obj inner join syscolumns col on obj.id = col.id where obj.name = 'table_Name'
select col.name from sysobjects obj inner join syscolumns col on obj.id = col.id where obj.name = 'table_Name'
Wednesday, March 19, 2008
RequiredFieldValidator and Button Click Event
If you use RequiredFieldValidator control on your ASP.NET page, every button click will perform validation. If you have rest buttons on your page on whose click you dont want validation, then set CausesValidation property of button to false
Thursday, March 13, 2008
Passing values between ASP.NET pages
Today I learned how to pass values between two pages in ASP.NET. Lets say you have Page1.aspx and Page2.aspx and want to pass values from Page1.aspx to Page2.aspx. You can put link button control on Page1.aspx and on its click event you can write this code
string mydata = "88";
Response.Redirect("Page2.aspx?value=" + mydata);
The above code will open Page2.aspx and now in order to get the value, you can write code on PageLoad event or any other method in code-behind file of Page2.aspx as follows
string mydata = Request.QueryString["value"];
string mydata = "88";
Response.Redirect("Page2.aspx?value=" + mydata);
The above code will open Page2.aspx and now in order to get the value, you can write code on PageLoad event or any other method in code-behind file of Page2.aspx as follows
string mydata = Request.QueryString["value"];
Tuesday, March 11, 2008
Substring of DataList field
The label control which is bind to the field for DataList control of ASP.NET page can be truncated using following syntax
Eval("FieldName").ToString().Substring(1,20)
Eval("FieldName").ToString().Substring(1,20)
Monday, March 10, 2008
Traverse through SQL Table rows
Here is the code to traverse (iterate) through each row in SQL table...
DECLARE @i INT
SET @i = 1
WHILE (@i<= (SELECT count(*) FROM table_name) )
BEGIN
/* Your Code here...*/
SET @i=@i+1
END
DECLARE @i INT
SET @i = 1
WHILE (@i<= (SELECT count(*) FROM table_name) )
BEGIN
/* Your Code here...*/
SET @i=@i+1
END
Monday, March 03, 2008
Event Trigger on selection changed in the DropDownList
On ASP.NET page....In order to tigger the event when selection of the DropDownList changes...write code in SelectionIndexChanged event of the DropDownList and dont forget to enable AutoPostBack to True.
Empty TextBox Check using RequiredFieldValidator
In order to check if something is entered into textbox...just drag and drop RequiredFieldValidator user control and set its ControlToValidate property to the desired textbox. And enter the custom error message you like to display...
Now when text is not entered into the desired textbox and page is refreshed using button click event the custom message appears......
Now when text is not entered into the desired textbox and page is refreshed using button click event the custom message appears......
Absolute Positioning of User Controls on ASP.NET Page
In Order to position ASP.NET usercontrols in visual designer...select control and then Click ( in Visual Studio ) on Layout->Position->Absolute. And then select and move the control on ASP.NET page as you like..
RadioButton GroupName
Simple and Silly but still important...Today I learned how to choose only one radio button among the radio buttons that are placed on the ASP.NET page. Just give the same GroupName (just any text) property of all radio buttons which you either want to select.
Friday, February 29, 2008
Get Logged in Windows User Name in ASP.NET Page
In order to get username of windows user who is logged into ASP.NET website, I used
string winUserName = System.Web.HttpContext.Current.User.Identity.Name;
winUserName contains the name of the user along with domain name.
string winUserName = System.Web.HttpContext.Current.User.Identity.Name;
winUserName contains the name of the user along with domain name.
Wednesday, February 27, 2008
Access the Items of the DataList ASP.NET
Today I learned how to access the selected row items of the datalist. In ItemTemplate of datalist I have added the button and on button click event I can access the items (e.g. MyLabel item of datalist named MyDataList). The steps are as follows
1. Add the button to the Itemtemplate of Datalist
2. enter 'select' in the CommandName property of the button
3. In Datalist SelectedIndexChanged add the following code. The value contained in item MyLabel is stored in the Mystring.
MyDataList.DataBind();
string MyString;
MyString = ((Label)MyDataList.Items[MyDataList.SelectedIndex].FindControl("MyLabel")).Text;
1. Add the button to the Itemtemplate of Datalist
2. enter 'select' in the CommandName property of the button
3. In Datalist SelectedIndexChanged add the following code. The value contained in item MyLabel is stored in the Mystring.
MyDataList.DataBind();
string MyString;
MyString = ((Label)MyDataList.Items[MyDataList.SelectedIndex].FindControl("MyLabel")).Text;
Tuesday, February 26, 2008
ADO.NET Hello World !!!
Today I learned how to connect to Microsoft SQL database from ASP.NET page using ADO.NET.
Here is the Page Load method which connects to the database and assigns the values taken from the database to the dropdownlist control.
protected void Page_Load(object sender, EventArgs e)
{
// SQL Connection
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "Data Source=COMPUTERV22;Initial Catalog=CIMNET2;User ID=sa;Password=manager;";
//SQL Command
SqlCommand MyCommand = new SqlCommand();
MyCommand.Connection = MyConnection;
MyCommand.CommandText = "Select * from Employees";
//Open a connection to the database
MyConnection.Open();
// SQL Reader, place where rows from database is stored.
SqlDataReader MyReader;
MyReader = MyCommand.ExecuteReader();
// Read each record from the database
while (MyReader.Read())
{
DropDownList1.Items.Add(MyReader.GetString(1));
}
//Close the connection to the database
MyConnection.Close();
}
Here is the Page Load method which connects to the database and assigns the values taken from the database to the dropdownlist control.
protected void Page_Load(object sender, EventArgs e)
{
// SQL Connection
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "Data Source=COMPUTERV22;Initial Catalog=CIMNET2;User ID=sa;Password=manager;";
//SQL Command
SqlCommand MyCommand = new SqlCommand();
MyCommand.Connection = MyConnection;
MyCommand.CommandText = "Select * from Employees";
//Open a connection to the database
MyConnection.Open();
// SQL Reader, place where rows from database is stored.
SqlDataReader MyReader;
MyReader = MyCommand.ExecuteReader();
// Read each record from the database
while (MyReader.Read())
{
DropDownList1.Items.Add(MyReader.GetString(1));
}
//Close the connection to the database
MyConnection.Close();
}
Subscribe to:
Comments (Atom)