Tuesday, May 28, 2013

How to validate an Ajax control combo box has item selected or not?



First, you need to insert beside the combo box a custom validator like this,

<asp:CustomValidator ID="cv" runat="server" ControlToValidate="cmb" Text="*" ForeColor="Red" ValidateEmptyText="true" ClientValidationFunction="CustomValidator" Display="Dynamic" ErrorMessage="combo box is required!" CssClass="validator" ValidationGroup="valGrp"></asp:CustomValidator>

If your combo box default selected value is an empty string then you need to add a function inside a <script type="text/javascript"> like this,

    function CustomValidator(s, e) {
        if ($find(s.controltovalidate)._textBoxControl.value == "") {
            e.IsValid = false;
        }
    }

If your combo box default value of “Please select” then you need to add a function inside a <script type="text/javascript”> like this,

    function CustomValidator(s, e) {
        if ($find(s.controltovalidate)._textBoxControl.value == "Please select") {
            e.IsValid = false;
        }
    }

References used:

Monday, May 13, 2013

How fix the column width in a gridview at insert tooltip in each cell in Asp.net 4?




If you need to set the width of each column in a gridview control,  If the column is just to display and no editing needed. You can like this,

<asp:TemplateField HeaderText="column Name" SortExpression="fieldname">
<HeaderTemplate>
       <div style="text-align:center;background-color:#41627E;color:White;font-size:x-small;font-weight:bold;overflow: hidden;white-space: nowrap;width: 50px;" >column Name</div>
</HeaderTemplate>
<ItemTemplate>
<div style="font-size:xx-small;white-space:nowrap;overflow: hidden;text-align:left;font-weight:600;font-family:Tahoma;width: 50px;" >
 <asp:Label ID="lblGridColumnName" runat="server" Text='<%# Bind("fieldname") %>' ></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>


But if there is a editing to be done, you need to do like this,

<asp:TemplateField HeaderText="column Name" SortExpression="fieldname">
<HeaderTemplate>
<div style="text-align:center;background-color:#41627E;color:White;font-size:x-small;font-weight:bold;overflow: hidden;white-space: nowrap;width: 40px;" >column name</div>
</HeaderTemplate>
<ItemTemplate>
<div style="font-size:xx-small;white-space:nowrap;overflow: hidden;text-align:left;font-weight:600;font-family:Tahoma;width: 40px;" >
<asp:Label ID="lblGridColumnName" runat="server" Text='<%# Bind("fieldname", "{0:n0}") %>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
              <asp:TextBox ID="txtGridColumnName" runat="server" Text='<%# Eval("fieldname", "{0:n0}") %>' Width="50px" CssClass="cssclass" />
       </EditItemTemplate>
</asp:TemplateField>


If you need insert a tooltip for each cell inside a gridview, you need to set each column tooltip. You can do like this,

protected void gv_DataBound(object sender, EventArgs e)
{
Label lbl = new Label();
foreach (GridViewRow rv in gv.Rows)
{
lbl = (Label)rv.FindControl("lblGridColumnName ");
rv.Cells[1].ToolTip = lbl == null ? string.Empty : lbl.Text;
}
}