Pages

Thursday, 17 May 2012

how to resize the textbox height dynamically or how to change or increase the textbox height dynamically based on text using javascript


Introduction:

In this article I will explain how to change or increase the textbox height dynamically using asp.net

Description:

I have one textbox my requirement is if user enters text in texbox that height of textbox needs to be adjusting with text for that I have written one JavaScript function to adjust textbox height here one more point is we need to set the
TextMode="MultiLine" property for textbox to adjust textbox height
Now design your aspx page like this



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Automatic Resize TextBoxtitle>
<script type="text/javascript">
function setHeight(txtdesc) {
txtdesc.style.height = txtdesc.scrollHeight + "px";
}
script>
head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtDesc" runat= "server" TextMode="MultiLine" onkeyup="setHeight(this);"onkeydown="setHeight(this);" />
</form>
</body>
</html>

Change Background-color of Div after Clik on LinkButton in Repeater..



Lets take an Example

 <asp:Repeater ID="rptCategory" runat="server">
 <HeaderTemplate>
           <h2  style="margin-bottom: 5px; color: #008752!important;font-size:16pt;">
                                                Course Categories</h2>
 </HeaderTemplate>
 <ItemTemplate>
           <table cellpadding="2px" cellspacing="2px">
           <tr>
                 <td>
                 <div runat="server" id="divleft" onmouseover="this.style.background='#BADFCE';" onmouseout="this.style.background='white';">
                   <asp:LinkButton ID="lnkCategory" runat="server" Text='<%#Eval("CategoryDescription")%>'
                   CommandArgument='<%# Eval("CourseLibraryCategoryID") %>' OnClick="lnkCategory_Click">
                    </asp:LinkButton>
                    </div>
                    </td>
            </tr>
            </table>
            </ItemTemplate>
            <FooterTemplate>
            </FooterTemplate>
 </asp:Repeater>


Answer:


 protected void lnkCategory_Click(object sender, EventArgs e)
  {
        
           //This is the code For Removing all style of Repeater Item
            foreach (RepeaterItem item in rptCategory.Items)
            {
              
                LinkButton previous = item.FindControl("lnkCategory") as LinkButton;
              
                (previous.NamingContainer.FindControl("divleft") as System.Web.UI.HtmlControls.HtmlContainerControl).Attributes.Remove("style");
                previous.Attributes.Remove("style");
            
            
            }
            LinkButton temp = (sender as LinkButton);
            temp.Attributes.Add("style", "color:green  !important;");
            (temp.NamingContainer.FindControl("divleft") as System.Web.UI.HtmlControls.HtmlContainerControl).Attributes.Add("style", "background-color:#BADFCE !important;");
}

Wednesday, 16 May 2012

Details view example in Asp.net



<asp:DetailsView ID="DetailsView1" runat="server" Width="100%"
        AutoGenerateRows="False" EmptyDataText="No Data Found"
        GridLines="None" onitemcommand="DetailsView1_ItemCommand"
        onitemdeleting="DetailsView1_ItemDeleting"
        onitemupdating="DetailsView1_ItemUpdating"
        onmodechanging="DetailsView1_ModeChanging">
        <FooterStyle Font-Bold="True" />
        <CommandRowStyle Font-Bold="True" />
        <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        <FieldHeaderStyle Font-Bold="True" />
        <EmptyDataRowStyle Font-Bold="True" />
        <PagerStyle HorizontalAlign="Left" />
        <Fields>          
            <asp:TemplateField ShowHeader="false" >
                <EditItemTemplate>
                    <asp:Label ID="lblIdEdit" runat="server" Text='<%# Bind("CntId") %>' Visible="false"></asp:Label>
                            <cc2:Editor ID="EditorEdit" runat="server" Content='<%# Bind("Description") %>' />
                </EditItemTemplate>              
                <ItemTemplate>
                    <asp:Label ID="lblId" runat="server" Text='<%# Bind("CntId") %>' Visible="false"></asp:Label>
                            <asp:Label ID="lblDetails" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ButtonType="Image" EditImageUrl="~/Images/Buttons/Admin_edit_btn.gif" UpdateImageUrl="~/Images/Buttons/admin_update_btn.gif" CancelImageUrl="~/Images/Buttons/Cancel_btn.gif" ShowEditButton="true" DeleteImageUrl="~/Images/Buttons/delete_btn.gif" ShowDeleteButton="true" />      
        </Fields>
        <HeaderStyle Font-Bold="True" ForeColor="White" />
        <EditRowStyle />      
    </asp:DetailsView>


C# code:-----------------------------
-----------------------------------------------------------------------------------------------------------
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            viewcontact();
        }
    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        string lblid = ((Label)DetailsView1.FindControl("lblId")).Text.ToString();
        obj1.deletecontactus(Convert.ToInt32(lblid));
        viewcontact();
        pnlCnt.Visible = true;
        pnlgrd.Visible = false;
       
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        try
        {
            string Id = ((Label)DetailsView1.FindControl("lblIdEdit")).Text.ToString();
            string Remarks = ((Editor)DetailsView1.FindControl("EditorEdit")).Content.ToString();
            obj1.updatecontactus(Convert.ToInt32(Id), Remarks);
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);          
            viewcontact();
        }
        catch (Exception ex)
        {
           
        }
    }
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        if (e.CancelingEdit == true)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            viewcontact();
        }
    }  

How to generate Random Password


private void RandomPassword()
    {
        string allowedChars = "";

        allowedChars = "1,2,3,4,5,6,7,8,9,0";

        char[] sep = { ',' };
        string[] arr = allowedChars.Split(sep);

        string passwordString = "";

        string temp = "";

        Random rand = new Random();
        for (int i = 0; i < 5; i++)
        {
            temp = arr[rand.Next(0, arr.Length)];
            passwordString += temp;
        }
        strPwd = passwordString;
    }

How to send mail from Asp.net


private int SendMail()
    {
        int result = 0;
        try
        {


            MailMessage objMail = new MailMessage();
            objMail.To = mailid.ToString().Trim();
            objMail.From = ConfigurationManager.AppSettings["FromMail"].ToString();
            objMail.Subject = "sriraj";
            objMail.BodyFormat = MailFormat.Html;

            objMail.Body = CreateBody();
            SmtpMail.SmtpServer = ConfigurationManager.AppSettings["Mailserver"].ToString();
            SmtpMail.Send(objMail);
        }
        catch (Exception ex)
        {
            throw ex;

        }
        return result;
    }

    private string CreateBody()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("Dear " + Session["companyName1"].ToString());
        sb.Append("<br><br><br>");
        sb.Append("Your Login details :");
        sb.Append("<br><br>");
        sb.Append("Login Id :" + txtEmailid.Text.Trim() + "<br/> Password :" + pwd.ToString());

        sb.Append("<br>");

        sb.Append("<br>");
        return sb.ToString();
    }

Change the status of record in gridview (active r Inactive)


protected void grdCategory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView row = (DataRowView)e.Row.DataItem;
            DataRowView rowStatus = (DataRowView)e.Row.DataItem;

            LinkButton lnkStatus = (LinkButton)e.Row.FindControl("lnkStatus");
            lnkStatus.Attributes["CompanyId"] = row["CompanyId"].ToString();
            LinkButton lnkbtnDelete = (LinkButton)e.Row.FindControl("lnkbtnDelete");
            lnkbtnDelete.Attributes["CompanyId"] = row["CompanyId"].ToString();


            if (rowStatus["Status"].ToString() == "Y")
            {
                lnkStatus.Text = "Active";
            }
            else if (rowStatus["Status"].ToString() == "N")
            {
                lnkStatus.Text = "InActive";
            }
        }
    }
    protected void lnkStatus_Click(object sender, EventArgs e)
    {

        string strCategoryId = ((LinkButton)sender).Attributes["CompanyId"];
        string intStatus;
        if (((LinkButton)sender).Text == "Active")
        {
            intStatus = "N";
        }
        else
        {
            intStatus = "Y";
        }
        SqlCommand cmd = new SqlCommand("UpdateUserLoginStatus", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter p1;
        p1 = new SqlParameter("@CompanyId", SqlDbType.Int);
        p1.Value = Convert.ToInt32(strCategoryId.ToString());
        cmd.Parameters.Add(p1);

        p1 = new SqlParameter("@Status", SqlDbType.VarChar);
        p1.Value = intStatus.ToString();
        cmd.Parameters.Add(p1);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        //(new R_Admin()).UpdateStatus(intStatus, "R_Categories", int.Parse(strCategoryId));
        getUserDetails();
    }
    protected void lnkbtnDelete_Click(object sender, EventArgs e)
    {
        string strSId = ((LinkButton)sender).Attributes["CompanyId"];
        //int intSId = (new R_Admin()).Deleting("R_Categories", Int32.Parse(strSId.ToString()));
        string s = "select * from CompanyLoginDetails where CompanyId=" + Convert.ToInt32(strSId.ToString());
        SqlCommand cmd1 = new SqlCommand(s, con);

        con.Open();
        cmd1.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd1;
        da.Fill(ds, "get");
        if (ds.Tables[0].Rows.Count > 0)
        {
            Session["companyid"] = Convert.ToInt32(ds.Tables[0].Rows[0]["CompanyMainId"].ToString());
        }



        SqlCommand cmd = new SqlCommand("DeleteUserLoginDetails", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter p1;
        p1 = new SqlParameter("@CompanyId", SqlDbType.Int);
        p1.Value = Convert.ToInt32(strSId.ToString());
        cmd.Parameters.Add(p1);

        cmd.ExecuteNonQuery();

        SqlCommand cmd2 = new SqlCommand("ChnageapproveStatus", con);
        cmd2.CommandType = CommandType.StoredProcedure;
        SqlParameter p2;
        p2 = new SqlParameter("@Cid", SqlDbType.Int);
        p2.Value = Convert.ToInt32(Session["companyid"].ToString());
        cmd2.Parameters.Add(p2);
        p2 = new SqlParameter("@Approved", SqlDbType.Char);
        p2.Value = "N";
        cmd2.Parameters.Add(p2);

        cmd2.ExecuteNonQuery();



        SqlCommand cmd3 = new SqlCommand("UpdateDirectoryStatus", con);
        cmd3.CommandType = CommandType.StoredProcedure;
        SqlParameter p3;
        p3 = new SqlParameter("@Cid", SqlDbType.Int);
        p3.Value = Convert.ToInt32(Session["companyid"].ToString());
        cmd3.Parameters.Add(p3);

        p3 = new SqlParameter("@Status", SqlDbType.VarChar);
        p3.Value = "N";
        cmd3.Parameters.Add(p3);

        cmd3.ExecuteNonQuery();
        con.Close();


        getUserDetails();

        pnlCommettee.Visible = true;
        lblMessageCommettee.Text = "<script>" + Environment.NewLine + "alert('Record Deleted successfully')</script>";
        pnlCommettee.Controls.Add(lblMessageCommettee);
    }
------------------------------------sriraj-----------------------------------------