Thursday, June 28, 2012

ASP.NET HTML text input field get and set value

In our asp.net application if we use html text input field for jquery or like other as we need we can

Get the input field value :

  <input type="text" name="std" id="datepicker5" />  

in C#

  string startDatepicker = String.Format("{0}", Request.Form["std"]);  


Set the input Field :

  <input type="text"  id="datepicker5" value="<%= this.FromDate %>"/>  

in C#

    // declare a from attribute   
     protected string FromDate { get; set; }  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       // setting value  
        this.FromDate = DateTime.Now.ToShortDateString();  
     }  


              Here it is important till now i not get any solution that set and get will work on same HTML text input field.

Tuesday, June 19, 2012

ASP.NET Jquery datepicker

In VS2010 the date time picker control is not available in toolbox. Their is only calender control. but if need to use date picker control in our form stylist j query date picker we can easily do it. let see some stylist datepicker from http://jqueryui.com/themeroller/ 




 You can also choose much more from http://jqueryui.com/themeroller/ . select which theme you want to use and download it. extract the zip file.  after extract you will see the file as like


we will use the css and the js folder data in our application.

Open visual studio 2010 select a new ASP.NET web application, after creating the project add the js and css from the downloaded file.


   In your master page head  show the reference of the js and css file and write the java script for .your datepicker.here i write 2 script named datepicker1 and datepicker2 for two datepicker control.


 <head runat="server">  
   <title></title>  
   <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />  
    <link href="~/Styles/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />  
     <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>  
   <script src="Scripts/jquery-ui-1.8.21.custom.min.js" type="text/javascript"></script>  
   <%--/* First script for first datepicker name datepicker1 */--%>  
   <script type="text/javascript" language="javascript">  
     $(document).ready(function () {  
       $("#datepicker1").datepicker();  
     });  
   </script>  
    <%--/* First script for first datepicker name datepicker2 */--%>  
    <script type="text/javascript" language="javascript">  
      $(document).ready(function () {  
        $("#datepicker2").datepicker();  
      });  
   </script>  
   <asp:ContentPlaceHolder ID="HeadContent" runat="server">  
   </asp:ContentPlaceHolder>  
 </head>  

Now in Default.aspx file i take a table with 2 row and 2 column .


 <table class="style1" align="center">  
     <tr>  
       <td class="style2">  
         <asp:Label ID="Label1" runat="server" Text="Start Date" Font-Bold="True"></asp:Label>  
       </td>  
       <td>  
          <div class="style3">  
        <input id="datepicker1" type="text" />  
       </div>   
         &nbsp;  
       </td>  
     </tr>  
     <tr>  
       <td class="style2">  
         <asp:Label ID="Label2" runat="server" Text="End Date" Font-Bold="True"></asp:Label>  
       </td>  
       <td>  
          <div class="style3">  
        <input id="datepicker2" type="text" />  
       </div>  
         &nbsp;  
       </td>  
     </tr>  
   </table>  

here i took 2 input control id name datepicker1 and datepicker1 and both's type is text. which is directly catch up our javascript. now run the application you will see both on click.







to Get data from the input field in C# you can write :


  string startDatepicker = String.Format("{0}", Request.Form["startDate"]);  
       string endDatepicker = String.Format("{0}", Request.Form["endDate"]);  


Enjoy...

ASP.NET Gridview cell value alignment

we can easily align our gridview cell value on load event like this

  for (int i = 0; i < damageDetailsGridView.Rows.Count; i++)  
         {  
           damageDetailsGridView.Rows[i].Cells[0].HorizontalAlign = HorizontalAlign.Center;  
           damageDetailsGridView.Rows[i].Cells[1].HorizontalAlign = HorizontalAlign.Left;  
           damageDetailsGridView.Rows[i].Cells[2].HorizontalAlign = HorizontalAlign.Center;  
         }  

the output of the grid look like


Enjoy..

Sunday, June 17, 2012

how to remove time from datetime and display in a gridview

When we  load a Gridview from database value. by default it shows time with date in gridview column.
look like






if we want to show only date without the time then we have to add 2 property in column binding. HtmlEncode="False" DataFormatString="{0:d}"

The gridview aspx bind will be

  <Columns>  
             <asp:BoundField DataField="DistributorId" HeaderText="Distributor Id" />  
             <asp:BoundField DataField="Status" HeaderText="Status" />  
             <asp:BoundField DataField="ProductLine" HeaderText="Product Line" />  
             <asp:BoundField DataField="OrderID" HeaderText="Order Id" />  
             <asp:BoundField DataField="OrderDate" HeaderText="Order Date" HtmlEncode="False"  
               DataFormatString="{0:d}" />  
             <asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" />  
             <asp:BoundField DataField="MessageId" HeaderText="Message Id" />  
             <asp:BoundField DataField="SendDate" HeaderText="Send Date" HtmlEncode="False" DataFormatString="{0:d}" />  
           </Columns>  

the the gridview will llok like


Sunday, June 10, 2012

ASP.NET Dropdown item selected as my given value

if we want set the selected item of a drop down box from my given value we can use follow the code. here i am taking a gridview value and set it as selected item of the drop down. cosider the image



aspx :

Wing Dropdown :
 
 <asp:DropDownList ID="regionWingDropDownList" runat="server" Height="30px" Width="217px"  
           DataValueField="WingId" DataTextField="WingName" AutoPostBack="True" Style="margin-top: 0px"  
           OnSelectedIndexChanged="regionWingDropDownList_SelectedIndexChanged">  
         </asp:DropDownList>  

Division Dropdown


 <asp:DropDownList ID="regionDivisionDropDownList" runat="server" Height="30px" DataValueField="DivisionId"  
           DataTextField="DivisionName" Width="217px">  
         </asp:DropDownList>  


Region Gridview


 <asp:GridView ID="regionGridView" runat="server" Width="457px"   
           AutoGenerateColumns="False"   
           onselectedindexchanged="regionGridView_SelectedIndexChanged">  
           <Columns>  
              <asp:BoundField DataField="RegionId" HeaderText="ID" />  
             <asp:BoundField DataField="RegionCode" HeaderText="Region Code" />  
             <asp:BoundField DataField="RegionName" HeaderText="Region Name" />  
             <asp:BoundField DataField="WingId" />  
             <asp:BoundField DataField="Wingname" HeaderText="Wing Name" />  
             <asp:BoundField DataField="DivisionId" />  
             <asp:BoundField DataField="DivisionName" HeaderText="Division Name" />  
             <asp:CommandField ShowSelectButton="True" />  
           </Columns>  
         </asp:GridView>  

Code behind :

  protected void regionGridView_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       if (regionGridView.SelectedIndex>-1)  
       {  
// setting value on dropdown
         for (int i = 0; i < regionWingDropDownList.Items.Count; i++)  
         {  
           ListItem wing = regionWingDropDownList.Items[i];  
           if (wing.Text == regionGridView.SelectedRow.Cells[4].Text)  
           {  
             regionWingDropDownList.SelectedIndex = i;  
           }  
         }  
       
         for (int i = 0; i < regionDivisionDropDownList.Items.Count; i++)  
         {  
           ListItem division = regionDivisionDropDownList.Items[i];  
           if (division.Text == regionGridView.SelectedRow.Cells[6].Text)  
           {  
             regionDivisionDropDownList.SelectedIndex = i;  
           }  
         }  
         regionCodeTextBox.Text = regionGridView.SelectedRow.Cells[1].Text;  
         regionNameTextBox.Text = regionGridView.SelectedRow.Cells[2].Text;  
         regionIdLabel.Text = regionGridView.SelectedRow.Cells[0].Text;  
         regionSaveButton.Text = "update";  
       }  
     }  

Thursday, June 7, 2012

ASP.NET Dropdown box event fire

Normally Dropdown box is not fire event. to fire any event of dropdown box you need to write the AutoPostBack="True" property in dropdown control.

ASPX File:

  <asp:DropDownList ID="regionWingDropDownList" runat="server" Height="30px" Width="217px"  
           DataValueField="WingId" DataTextField="WingName" AutoPostBack="True"  
           style="margin-top: 0px"   
           onselectedindexchanged="regionWingDropDownList_SelectedIndexChanged"   
          >  
         </asp:DropDownList>  

Codebehind:
 protected void regionWingDropDownList_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       Wing wing = new Wing();  
       wing.WingId = Convert.ToInt32(regionWingDropDownList.SelectedItem.Value);  
       regionDivisionDropDownList.DataSource = _regionManagerObj.GetSelectedWingAllDiviSionName(wing);  
       regionDivisionDropDownList.DataBind();  
     }  

ASP.NET Gridview Column Hide

If you want to hide any column Of a grid you have hide it after load the grid.






Now i want to hide the id Column which contain 1,2,3,4.....

then the code will be on load event of the grid :


 private void LoadDivisionGridData()  
     {  
       _divisionslist = new List<Division>();  
       _divisionslist = _divisionManagerObj.GetAllDivisionInfo();  
       divisionGridView.DataSource = _divisionslist;  
       divisionGridView.DataBind();  
       ResetAllField();  
       divisionGridView.Columns[0].Visible = false;  
     }  


after hide the Id column it will be look life



 Thank you.



Wednesday, June 6, 2012

How to get ASP.NET Gridview selected row value


Gridview binding is one of the important to get selected row value from a asp.net Grid view. consider the following ASP.NET form i want to insert data that will save in SQL database and the Gridview will be load. Now i want when i will click on Select in Gridview button , selected row value will be load on Text field for update.









  Class File:

 public class Wing  
   {  
     public int WingId { get; set; }  
     public string WingName { get; set; }  
     public string WingCode { get; set; }  
   }  



Datagrid binding in aspx

 <asp:GridView ID="wingsGridView" runat="server" Width="500px" AutoGenerateColumns="False"  
           OnSelectedIndexChanged="wingsGridView_SelectedIndexChanged">  
           <Columns>  
             <asp:BoundField DataField="WingId" HeaderText="Id" />  
             <asp:BoundField DataField="WingCode" HeaderText="Code" />  
             <asp:BoundField DataField="WingName" HeaderText="Name" />  
             <asp:CommandField ShowSelectButton="True" />  
           </Columns>  
         </asp:GridView>  



code behind code :


 protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         LoadWingDataGrid();  
         wingIdLabel.Visible = false;  
       }  
     }  
     private void LoadWingDataGrid()  
     {  
       wings = new List<Wing>();  
       wings = wingManagerObj.GetAllWingsName();  
       if (wings.Count > 0)  
       {  
         wingsGridView.DataSource = wings;  
         wingsGridView.DataBind();  
         wingsGridView.Visible = true;  
         ResetAllField();  
       }  
     }  
  protected void wingsGridView_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       try  
       {  
        Wing  _wingObj = new Wing();  
         _wingObj.WingId = Convert.ToInt32(wingsGridView.SelectedRow.Cells[0].Text);  
         _wingObj.WingCode = wingsGridView.SelectedRow.Cells[1].Text;  
         _wingObj.WingName = wingsGridView.SelectedRow.Cells[2].Text;  
         wingCodeTextBox.Text = _wingObj.WingCode;  
         wingNameTextBox.Text = _wingObj.WingName;  
         wingIdLabel.Text = _wingObj.WingId.ToString();  
         wingInSaveButton.Text = "Update";  
       }  
       catch (Exception exception)  
       {  
         messageLabel.Text = exception.Message;  
       }  
     }  

If you have any query please replay.


SQL Get all Index create script from Database

To get all script from database as a create new index into another database you can use the following --Get all Index Script SELECT...