Thursday, March 6, 2014

Javascript alert message from controller to view in MVC3

In controller action:

if(success)
{
    TempData["Alert"] = "Alert message";    
}

The view which SomeAction action returns (or in layout view):


@{
    var message = TempData["Alert"] ?? string.Empty;
 }

<script type="text/javascript">
    var message = '@message';
    if(message)
        alert(message);
</script>
Posted on 9:31 PM | Categories:

Wednesday, June 29, 2011

Gridview Inside a GridView in ASP.NET 4.0



Under showdata button_click
protected void Button1_Click(object sender, EventArgs e)
{
   try
    {
    Utility obj = new Utility();
    GridView1.DataSource = obj.Getrest();
    GridView1.DataBind();
    for (int i = 0; i < GridView1.Rows.Count; i++)
      {
        GridView gv = (GridView)GridView1.Rows[i].FindControl("GridView2");
        Label bv = (Label)GridView1.Rows[i].FindControl("lbl");
        gridviewModel.menu obj1=new gridviewModel.menu ();
        gv.DataSource = obj.Getmenu(bv.Text);
        gv.DataBind(); 
      }
    }
   catch (Exception ex)
   {
   Response.Write(ex.Message);
   }
 }

Gridview Source Code:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  Height="183px" Width="584px" CellPadding="4" ForeColor="#333333"
  GridLines="None">
  <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  <Columns>                       
  <asp:TemplateField HeaderText ="Rid">
  <ItemTemplate>
  <asp:Label runat="server" ID="lbl"  
  Text='<%#Eval("Rid") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Hotel Name">
  <ItemTemplate>
  <asp:Label runat="server" ID="lbl1" Text='<%#Eval("name") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Location">
  <ItemTemplate>
  <asp:Label runat="server" ID="lbl2" 
  Text='<%#Eval("location") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Menus">
  <ItemTemplate>
  <asp:GridView ID="GridView2" runat="server"  AutoGenerateColumns="false"
  style="margin-right: 2px"
  Width="369px" BackColor="White"
  BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
  <Columns>                       
  <asp:TemplateField HeaderText="Mid">
  <ItemTemplate>
  <asp:Label ID="Label1" runat="server" 
  Text='<%#Eval("mid") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Item Name">
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server"           
  Text='<%#Eval("itemname")%>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Price">
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server"  
  Text='<%#Eval("price") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>                                           
  </Columns>
  <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  <HeaderStyle BackColor="Indigo" Font-Bold="True" ForeColor="#CCCCFF" />
  <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left"/>
  <RowStyle BackColor="White" ForeColor="#003399" />
  <SelectedRowStyle BackColor="#009999" Font-Bold="True"
  ForeColor="#CCFF99"/>
  <SortedAscendingCellStyle BackColor="#EDF6F6" />
  <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
  <SortedDescendingCellStyle BackColor="#D6DFDF" />
  <SortedDescendingHeaderStyle BackColor="#002876" />
  </asp:GridView>
  </ItemTemplate>
  </asp:TemplateField>
  </Columns>
  <EditRowStyle BackColor="#999999" />
  <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
  <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"/>
  <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"/>
  <SortedAscendingCellStyle BackColor="#E9E7E2" />
  <SortedAscendingHeaderStyle BackColor="#506C8C" />
  <SortedDescendingCellStyle BackColor="#FFFDF8" />
  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
  </asp:GridView>

Add ADO.NET Entity Data Model under App_code and select database tables







  add new class Utility.cs and write code under utility class
Under Utility Class:-
 public List<gridviewModel.rest> Getrest()
   {
      gridviewModel.gridviewEntities obj = new gridviewModel.gridviewEntities();
      var x = from n in obj.rests select n;
      return x.ToList<gridviewModel.rest>();
   }
public List<gridviewModel.menu> Getmenu(object ss)
   {
      int s = int.Parse(ss.ToString());
      gridviewModel.gridviewEntities obj = new gridviewModel.gridviewEntities();
      var c=from n in obj.menus where n.Rid==s select n;
      return c.ToList<gridviewModel.menu>();
   }

Posted on 8:35 AM | Categories:

Monday, March 21, 2011

Regular Expression Validator Control


Regular Expression Validator Control:-
This is one of the powerful and useful validation control. These control perform validation based on the given resources in the form of expression. Expression or regular expression is a pattern of .NET provides System.Text.RegularExpressions namespace which contain many classes and methods to perform expression tasks .In ASP.NET regular expression validator control is provided which simplify the use of expression .ASP.NET provides regular expression editor which provides sample expression for use as well as for customization.
The Regular Expression Validator control allows you to check the value a user entered into a text box or other input control to ensure that it meets a particular form, such as a social security number, a phone number, and email address, etc.
         1. \d-digit
         2.\w-Character
         3.{}-Range of Character

        eg.1)PR123INV#123
      solution:-[A-Z a-z]{2}\d{3}[A-Z a-z]{3}#\d{3}                


How to use Maximum and minimum values between TextBox in Asp.Net?
Solution:-
Step 1)Drag and drop RegularExpressionValidator
Step 2)Go to properties window and first set ControlTovalidate as TextBox
Step 3)select another properties ValidationExpression and click.After clicking new window will open this windows is known as Regular Expression Editor and go to below Validation Expression and write
 \w{6,30}.means character range between 6 to 30.

Validation of Email Id:-
Step 1:
Step 2:
Step 3:-
                              save it,run and check out output.
Posted on 3:03 AM | Categories:

Monday, August 9, 2010

Calculate Total value in Dynamically generated textbox

Text box dynamically in a Gridview in Asp.net:-

        <asp:GridView ID="GridView5" runat="server" CellPadding="4"
        EnableModelValidation="True" Width="1115px" AutoGenerateColumns="False"            
        BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
        <RowStyle BackColor="White" ForeColor="#003399" />
        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <Columns>
        <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
        <asp:Label ID="CId3" runat="server" Text=''> </asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Student Name">
        <ItemTemplate>
        <asp:Label ID="CName3" runat="server" Text='' Width="120"></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Return">
        <ItemTemplate>
        <asp:TextBox ID="TextBox36" runat="server" Width="60px"></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="1Mile/800 Meter">
        <ItemTemplate>
        <asp:TextBox ID="TextBox37" runat="server" Width="60px">
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Marks">
        <ItemTemplate>
        <asp:TextBox ID="TextBox38" runat="server" Width="60px" >
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Long Jump">
        <ItemTemplate>
        <asp:TextBox ID="TextBox39" runat="server" Width="60px"></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Marks">
        <ItemTemplate>
        <asp:TextBox ID="TextBox40" runat="server" Width="60px"></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="High Jump">
        <ItemTemplate>
        <asp:TextBox ID="TextBox41" runat="server" Width="60px"></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Mark">
        <ItemTemplate>
        <asp:TextBox ID="TextBox42" runat="server" Width="60px">
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Interview">
        <ItemTemplate>
 <asp:TextBox ID="TextBox43" runat="server" Width="60px"     
 onblur="validateField3(this)">
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
        <ItemTemplate>
        <asp:TextBox ID="TextBox44" runat="server" Width="60px" Enabled="false">
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

 

Codes foJavaScript:-

<script language ="javascript" type="text/javascript">
      function validateField3(fieldname)
      {
          if ((fieldname.value) && (!fieldname.value.match(" ")))
          {
            var str = fieldname.id1;
            document.getElementById(fieldname.id.replace("43", "44")).value =   

            parseInt(document.getElementById(fieldname.id.replace("43", "36")).value) +     
            parseInt(document.getElementById(fieldname.id.replace("43", "38")).value) + 
            parseInt(document.getElementById(fieldname.id.replace("43", "40")).value) + 
            parseInt(document.getElementById(fieldname.id.replace("43", "42")).value) + 
            parseInt(document.getElementById(fieldname.id.replace("43", "43")).value);
          }
        }   
    </script>


Data bind:-


protected void Button3_Click(object sender, EventArgs e)

{


       DataAccess da = new DataAccess();
       da.OpenConnection();
       string id;
       foreach (GridViewRow row in GridView5.Rows)
        {
         string c = ddmonth.SelectedValue.ToString() + "/"   
                    + ddday.SelectedValue.ToString() + "/"
                    + ddyear.SelectedValue.ToString();
               id = ((Label)row.FindControl("Cid3")).Text;
          TextBox text36 = (TextBox)row.FindControl("TextBox36");
          TextBox text37 = (TextBox)row.FindControl("TextBox37");
          TextBox text38 = (TextBox)row.FindControl("TextBox38");
          TextBox text39 = (TextBox)row.FindControl("TextBox39");
          TextBox text40 = (TextBox)row.FindControl("TextBox40");
          TextBox text41 = (TextBox)row.FindControl("TextBox41");
          TextBox text42 = (TextBox)row.FindControl("TextBox42");
          TextBox text43 = (TextBox)row.FindControl("TextBox43");
          TextBox text44 = (TextBox)row.FindControl("TextBox44");
           
       if (text36 .Text !="" && text37 .Text !="" && text38 .Text  !="" && text39 .Text !="" && text40 .Text !="" && text41 .Text !="" && text42 .Text !="" && text43 .Text !="")
      {
       try
        {
         double sa = (double.Parse(text36.Text) +     double.Parse(text38.Text) + double.Parse(text40.Text) + double.Parse(text42.Text) + double.Parse(text43.Text));
         da.ExecuteNonQuery("Insert into PhysicalPractice(id,pdate,Rtnmarks,Atasemeter,mark1,Longjump,mark4,Highjump,mark5,interview,total) values('" + id + "','" + c + "','" +text36 .Text + "','" +text37 .Text + "','" +text38 .Text + "','" +text39 .Text  + "','" +text40 .Text + "','" +text41 .Text + "','" +text42 .Text + "','" +text43 .Text + "','" + sa.ToString() + "')");
         lbl_msg4.Text = "Record Save successfully";
         }
         catch (Exception ex)
         {
           Response.Write(ex.Message);
         }
         }
         else
         {
          lbl_msg4.Text = "Please enter all inforamation";
         }

        }

    }

}
 



Posted on 10:53 AM | Categories: