Visible the Textbox when User Select Others on the Dropdownlist using Javascript

Posted by Venkat | Labels: ,

Hi i have came across one task, ie: I have dropdownlist that has some items along with 'Others' So if the user Choose the value Others -  i have to visible the Textbox and Validator so user should enter somthing on the textbox else it show Validation error if the user choose any other item , i have to disable the Textbox and the Validator.

Using Javascript its easy to do this , i am not good in Javascript i got a help from the Expert to make it work.

ASPX

<asp:DropDownList ID="EducationLevel_DropDownList" runat="server" Width="202px"
                                            Font-Names="Verdana" Font-Size="10pt" AppendDataBoundItems="True">
                                            <asp:ListItem Value="-1">Select</asp:ListItem>
                                            <asp:ListItem Value="1">PhD</asp:ListItem>
                                            <asp:ListItem Value="2">PhD Student</asp:ListItem>
                                            <asp:ListItem Value="3">Master Degree</asp:ListItem>
                                            <asp:ListItem Value="4">Bachelor Degree</asp:ListItem>
                                            <asp:ListItem Value="5">Under Graduate Student</asp:ListItem>
                                            <asp:ListItem Value="6">Others</asp:ListItem>
                                        </asp:DropDownList>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="EducationLevel_DropDownList"
                                            Display="Dynamic" ErrorMessage="Education Leve required !" InitialValue="-1"
                                            SetFocusOnError="True" ValidationGroup="Profile" Font-Size="8pt">*</asp:RequiredFieldValidator><asp:TextBox ID="TextBox1"
                                                runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                                            Display="Dynamic" ErrorMessage="Education Leve required !" 
                                            SetFocusOnError="True" ValidationGroup="Profile" Font-Size="8pt">*</asp:RequiredFieldValidator>

And the Javascript is given below so here i am pass the Valuefield of Other ie: 6 , when a textbox has validators, they are stored in the Validators collection , so no need to pass the ValidatorID.


JAVASCRIPT


<script type="text/javascript" language ="javascript" >


function getSelectValue(sel) {
   
    var val;
    // only if there is a selected item
    if (sel.selectedIndex>-1) {
        // get the value from the select element
        val=sel.options[sel.selectedIndex].value;
       // alert(val);
    }
   
    return val;
}


        function ddlChange(DropDownID,TextBoxID)
        {
            var c=document.getElementById(DropDownID);
            var div=document.getElementById(TextBoxID);
            if(getSelectValue(c)=='6')
            {
                div.value='';
                div.style.visibility="visible";
                if (div.Validators&&div.Validators.length) {
                   for (var i=0; i<div.Validators.length; i++)
                       ValidatorEnable(div.Validators[i], true)
                }
            }
            else
            {
                div.value='6';
                div.style.visibility="hidden";
                if (div.Validators&&div.Validators.length) {
                   for (var i=0; i<div.Validators.length; i++)
                       ValidatorEnable(div.Validators[i], false)
                }
            }
        }
    </script>

Then On_PageLoad you have to pass the TextboxID and DropdownlistID to the Javascript Function.


EducationLevel_DropDownList.Attributes.Add("onchange", "ddlChange('" + EducationLevel_DropDownList.ClientID + "','" + TextBox1.ClientID + "');");
        TextBox1.Style.Add("visibility", "hidden");

Bind Images from Folder to Datalist

Posted by Venkat | Labels: ,

Here we are going to see how to bind the images from the folder to Datalist. I am storing the images on Server Folder , so i have to show what are the images available on the Folder it may be .png,.jpg or any ...

ASPX

<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" CellPadding="5">
            <ItemTemplate>
            <asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/images/{0}") %>' runat="server" />
                <br />
                <asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/images/{0}") %>' runat="server"/>
            </ItemTemplate>
                <ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"
                    VerticalAlign="Bottom" />
</asp:DataList> 

see here, we get the files from the folder using System.IO Namesapce  , then i am checking the images on the folder ie: checking the extension that i am going to allow - to show it on Datalist. you can include the Extension whatever you want so that is going to add it on arrayList, then pass the array list to DataSource of the Datalist.


Code-Behind

protected void Page_Load(object sender, EventArgs e)
    {
        ListImages();
    }

    private void ListImages()
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("~/images"));
        FileInfo[] file = dir.GetFiles();
        ArrayList list = new ArrayList();
        foreach (FileInfo file2 in file)
        {
            if (file2.Extension == ".jpg" || file2.Extension == ".jpeg" || file2.Extension == ".gif")
            {
                list.Add(file2);
            }
        }
        DataList1.DataSource = list;
        DataList1.DataBind();
    }

Validate Checkbox and Checkboxlist

Posted by Venkat | Labels:

If we want to Validate the Textbox, Dropdownlist we used Validation Server Controls available in ASP.NET
Suppose if we want to validate Checboxlist or checkbox , some member validate through Javascript its good , some feed if its similar to Validator controls it should be easy to validate,.

so here is the link its similar to validation Server controls just Download the skmValidator Controls and add it on toolbox just drag and drop and set the errormessage , validationgroup, controltovalidate etc..

http://aspnet.4guysfromrolla.com/articles/092006-1.aspx 

If you want to do on Client side check this link

http://www.dotnetcurry.com/ShowArticle.aspx?ID=197&AspxAutoDetectCookieSupport=1

How to get Control Value From CreateUserWizard Control

Posted by Venkat | Labels: ,

Now I am going to discuss how to get the Controls Values ( ie:  Textbox , Dropdownlist etc.. ) inside the CreateUserWizard control, In CreateUserWizard Control on first view you can't able to get the Controls value directly.

Using FindControl i am getting the control value.

Write these Code on CreateUserWizard_CreatingUser Event

 DropDownList Questionddl = (DropDownList)CreateUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("Question_DropDownList");
CreateUserWizard.Question = Questionddl.SelectedValue;

Set the Value has Selected to Listbox by getting value from Database

Posted by Venkat | Labels:

As we already saw how to get the items(Multiple selection item) from the Listbox. Suppose If we store that value like this(2,4,6,) ie: I concatenate the (,) for each valu -  2,4,6 are the value of the listbox so we are going to rebind this value to List and set it has Selected.

Here the subject is the field on the Table where the [ 2,4,6, ] was stored.

1) First we get the database value to string Variable

2) Then using Split property - remove the (,) from the String and assign to StringArray

3) Atlast use foreach loop to set the item to be selected.

  string subjectList = getUserProfile_SqlDataReader["subject"].ToString();
                            string[] subjectListItems = subjectList.Split(',');
                            foreach (string s in subjectListItems)
                            {
                                foreach (ListItem item in Subject_ListBox.Items)
                                {
                                    if (item.Value == s) item.Selected = true;
                                }
                            }

ETHERPAD

Posted by Venkat |

I Came across new EtherPad , which user can chat and , also edit the code simaltaneously both users

This is my etherPad - Come Here Chat Here..


http://venkat.etherpad.com/1

How to download a file from Server folder

Posted by Venkat | Labels:

Now we are going to see how to download a file or image from the Server folder ie: showing file/open dialog box. For this we have to use Response object to show or get the file.

Sample Code:

        private void WriteFileToResponse()
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.MapPath("~/Download/" + FileName));
            //                    Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";

            //Write the file to the Response Output stream without buffering. 
            //This is new to ASP.NET 2.0. Prior to that use WriteFile. 
            Response.TransmitFile(Server.MapPath("~/Download/" + FileName));
            Response.Flush();
            Response.End();
        }

HTML Validation

Posted by Venkat | Labels: ,

As we used the Embed tag for embed the Flash files , on home or master page as this tag not support for HTML Validation (W3C) . but still we used this tag for showing flash file , if you want to do HTML validation for your site - this makes an error , so you have to avoid this.


Instead use Object tag for placing Flash files

Example:

 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="the-swf-file.swf" type="application/x-shockwave-flash" width="600" height="420"> <param name="movie" value="the-swf-file.swf" /> <param name="quality" value="high" /> </object>

Wrap the text on Gridview.

Posted by Venkat | Labels:

Hi Good day to All

After a long time , i am back  - i have no time to post the Resources here . now , i will try to post atleast one Resource per day.

I wish you all a Very Happy Diwali

Now i am going to see how to Wrap the text show in the Gridview , I had searched many links & sites finally i got one javascript.
place the javascript at the Top of the page ie: loaded when the page Loads.

<script type="text/javascript"> 
       window.onload = function()
        { 
        if (window.attachEvent == undefined) 
        {
         var tag = document.getElementsByTagName("td"); 
         for (var i = 0; i < tag.length; i++)
          {
           if (tag.item(i).className == "wordwrap") 
           {
            var text = tag.item(i).innerHTML; 
            tag.item(i).innerHTML = text.replace(/(.*?)/g, "<wbr/>"); 
            } 
           } 
          }
         }
    </script>
And one thing you are going to consider , here you have to apply the class="wordwrap" on your td tag at where you want text wrap or which column wants the text wrap then you have to specify the width in px not in % or any.
This is my Sample Gridview code

<asp:GridView ID="userList_GridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                          BorderColor="WhiteSmoke" BorderWidth="0px" CellPadding="5" 
                                            EmptyDataText="User not found...!" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
                                            PageSize="30" ToolTip="User List....!" Width="100%" GridLines="None" BackColor="WhiteSmoke" >
                                            <Columns>
                                                <asp:TemplateField>
                                                    <HeaderTemplate>
                                                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                                            <tr style=" color: Black;" class ="fbtxt">
                                                                <td align="left" style="width: 18%">
                                                                 Username
                                                                </td>
                                                                <td align="left" style="width: 14%">
                                                                    Email
                                                                </td>
                                                                
                                                                
                                                               <td align="center" style="width: 15%">
                                                                   Resource Permission
                                                                </td>
                                                                <td align="center" style="width: 11%">
                                                                    Permission
                                                                </td>
                                                                  <td align="center" style="width: 7%">
                                                                     Edit
                                                                    </td>
                                                                <td align="center" style="width: 8%">
                                                                    Delete
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </HeaderTemplate>
                                                    <HeaderStyle BackColor="LightSteelBlue"  Font-Bold="True" Font-Size="10pt" ForeColor="Black" />
                                                    <ItemTemplate>
                                                    <div style ="width :100%;table-layout:fixed;border:0px;">
                                                    <div class ="fntxt">
                                                    <div align="left" style="width: 165px; word-wrap:break; float:left ;" class="wordwrap">
                                                      <%# Eval("username")%>
                                                    </div> 
                                                    <div align="left" style="width: 150px;float:left;" class="wordwrap" >
                                                     <%#Eval("email")%>
                                                    </div> 
                                                    
                                                   <div align="left" style="width: 20%;float:left;" >
                                               <a href ='<%# "resourceaccess.aspx?userResource=" +  Eval("username")   %>' class ="WhiteLink"> Resource Access</a>
                                                    </div>
                                                      <div align="left" style="width: 13%;float:left;" >
                                                      <a href= '<%# "userlist.aspx?userPermission=" +  Eval("username")  %>' class ="WhiteLink"> <%# Eval("approve") %></a>
                                                 
                                                    </div>
                                                    <div align="center" style="width: 12%;float:left;"  >
                                                    <asp:HyperLink ID="Edit_HyperLink"  runat="server" NavigateUrl='<%# "~/Admin/userlist.aspx?editUser=" +  Eval("username")  %>'
                                                                      CssClass ="WhiteLink"  OnClick="return confirm('Are you sure want to Edit this record ?');" Text="Edit"
                                                                        ToolTip="Edit user list....!"></asp:HyperLink>
                                                    </div> 
                                                    <div align="center" style="width: 7%;float:left;"  >
                                                    <asp:HyperLink ID="Delete_Button"  runat="server" NavigateUrl='<%# "~/Admin/userlist.aspx?delUname=" +  Eval("username")  %>'
                                                                      CssClass ="WhiteLink"  OnClick="return confirm('Are you sure want to Delete this record ?');" Text="Delete"
                                                                        ToolTip="Delete user list....!"></asp:HyperLink>
                                                    </div> 
                                                    
                                                    </div>
                                                    </div>
                                                        
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                            <PagerStyle BackColor="WhiteSmoke" Font-Bold="False" Font-Names="verdana" Font-Size="8pt"
                                                ForeColor="Red" HorizontalAlign="Center" />
                                        </asp:GridView>

PayOffers.in