0

Repeater Example


Hi,
Following is a very basic example of repeater, My Table has two columns viz Val1 and Val2.
DataBinder.Eval(Container.DataItem, "Column_Name") here in the "" you will have to fill in your column name.

 <form id="form1" runat="server">
    <div>
        <b>Using DataBinder.Eval(Container.DataItem, "col_name") method</b>
        <asp:Repeater ID="Repeater1" runat="server" 
        DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <table style="width: 400px; font-family: Verdana; font-size: 20px;
                color: Orange;">
                    <tr style="background-color: #FFeFef;">
                        <td>
                            Hi, My name is
                            <%#DataBinder.Eval(Container.DataItem, "Val1")%>
                            <%#DataBinder.Eval(Container.DataItem, "Val2")%>
                         <%--<%# Eval("Col_Name") %> produces same result--%>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ConnectionStrings:MyDatabaseConnectionString %>"
            SelectCommand="SELECT * FROM [NewTable]"></asp:SqlDataSource>
    </div>
    </form>





0

Default button for ENTER key using C#

Sometimes you may want a button's on click event to occur on the press of the ENTER key.
In such cases there are a number of ways to do it.
I'll show you 2 of these many ways.


1. In your FORM element in your aspx page add an attribute

<form id="form1" runat="server" defaultbutton="Button1">
<div>
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</div>
<form>
2. On page_load method add:

Form.DefaultButton = Button_Name.UniqueID;


0

Remove HTML tags in a string using javascript

Hi, following code depicts how a string with HTML tags, is converted to a string without any tags.


<HTML> 
<HEAD> 
<TITLE> Remove HTML tags </TITLE> 
</HEAD> 
<BODY onload="demo();"> 
<SCRIPT LANGUAGE="JavaScript"> 
function demo()
{


var orignal= '<p align="Left"><b>I</b> <I>Rock!</I></p>'; 
var modified = orignal.replace(/(<([^>]+)>)/ig,""); 


alert(orignal);
alert(modified);


} 
</SCRIPT> 
</BODY> 
</HTML>


Keeps Coding! :)
0

Double Click event for a Row in a Grid View

1. First while on OnRowDataBound Method of your GridView insert the following:



if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < (e.Row.Cells.Count); i++)
{
e.Row.Attributes.Add("ondblclick", "Javascript:rowDblClick();");
}
}
2. Write a javascript method in your aspx page.

  function rowDblClick(){ 
        alert("done!");
      }
 Thats it you are done !
0

GridView Row Color Change Using CSS

First define the css which you want to use. This you could do by defining a seperate css file or by adding the css in the same file. I have declared the css in the same file. The head section of my .aspx file looks like below.

<head runat="server">
    <title>Untitled Page</title>
    <style>
     .RowCss tr:hover
    {
        background-color:Orange;
    }
    </style>
</head>

Next you have to give this css class to the Grid View where you need to change your row.

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
 DataSourceID="SqlDataSource1" CssClass="RowCss" >

Expected Output:
0

Check whether a HTML control exists or not


<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Check whether a control exists</title> 
</head> 
<body> 
   <script type="text/javascript">
       function check() {
           if (document.getElementById('btn_yasser')) {
               alert('Exists !');
           }
           else {
               alert('Not present');
           }
       }
   </script> 
   <input type="button" id="btn_yasser" value="click me" onClick="check();" /> 
</body> 
</html>
The same could be used for ASP controls.
Hope this helps.
0

Session Management For Grid View Control

I recently came across a situation, where I had to use session management for GRID VIEW CONTROL.


I had a search page where there was a grid displaying some data. Whenever a search was made, the data was fetched from the back end and displayed in the grid view. Now whenever the user navigates to another page and comes back to the search page, my gridview values should be still there. 
Use the code below in the appropriate place. 

// Store GridView in Session
Session["Test"] = GridView1;
GridView1 = null;


// Retrieve GridView from Session
GridView1 = (GridView)Session["Test"];


Hope this helps!
4

Add Row / Columns to GridView (Programmatically)


1) Create a DataTable object to which than we will bind the GridView

DataTable dt = new DataTable();
2) IF you need two columns than create two DataColumn objects

DataColumn dc1 = new DataColumn("first", typeof(string));

DataColumn dc2 = new DataColumn("second", typeof(string));
DataColumn dc3 = new DataColumn("third", typeof(string));

Add it to the table

dt.Columns.Add(dc1);

dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
3)Run the loop for as many rows you want to add. eg: 3 rows to be added.

for(int i=0;i<3;i++)// 3 = number of rows needed.

{
   DataRow row1 = dt.NewRow();
   int c = 0;
   while (c < 3)
   {
       row1[c] = "some_data";
       c++;
   }
   dt.Rows.Add(row1 );
}

Now iterate through each datacolumn and create a BoundField foreach column

foreach (DataColumn col in dt.Columns)

{
BoundField bField = new BoundField();
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}

GridView1.DataSource = dt;
//Bind the datatable with the GridView.
GridView1.DataBind();
And you have successfully added rows/cols to the grid !
0
Database Basics using C#:


The following demonstrates a Register Page where the user is allowed to register, by entering a username and password. These inputs are saved in a table in database and later verified when user wants to login using the login page.

Section I:
Code for Register Page

    String ConnString = “--Your Connection_String goes here--";
    SqlConnection con = new SqlConnection(ConnString);

    String Command = ”insert into TABLE_NAME values(@username,@password)”;
    SqlCommand cmd = new SqlCommand(command, con);

    con.Open();

    cmd.Parameters.Add(“@username”, SqlDbType.Text).Value = Textusername.Text;
    cmd.Parameters.Add(“@password”, SqlDbType.Text).Value = Textpassword.Text;

    int a = cmd.ExecuteNonQuery();

    if (a > 0)
    {
        Response.Redirect(“~/Login.aspx”);
    }
    else
    {
        lablel.ForeColor = System.Drawing.Color.Red;
        label.Text = “Error Occured!”;
    }

Section II:
Code for Login Page


SqlConnection con = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter(“select * from userinfo”, con);
DataSet ds = new DataSet();
da.Fill(ds, “info”);

string username = Textusername.Text;
string password = Textpassword.Text;

int find = 0;
foreach (DataRow dr in ds.Tables["info"].Rows)
{
    if (username == dr["username"].ToString())
    {
        if (password == dr["password"].ToString())
        {
        find = 1;
        break;
        } // end of 1st if
    }// end of 2nd if
} // end of for each

if (find == 1)
{
    Response.Redirect(“~/Home.aspx”);
}
else
{
    Labelerror.ForeColor = System.Drawing.Color.Red;
    Labelerror.Text = “Username Password Mismatch”;
}



0

SET PATH IN WINDOWS 7


Setting Path on Windows 7
  1. Start -> Control Panel -> System -> Advanced
  2. Click on Environment Variables, under System Variables, find PATH, and click on it.
  3. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
  4. Close the window.
 Hope this helps! :)
 

2011 ·Code-Studio by yrus.