Next textbox disable in ASP.net - asp.net

I want to add one validation like if Status is "Accept" then QtyRejected get disable
Below is my ASP code for status and qtyRejected
<td>
<asp:DropDownList ID="lAccept" runat="server" Height="25px" Width="102px">
<asp:ListItem>Accept</asp:ListItem>
<asp:ListItem>Reject</asp:ListItem>
</asp:DropDownList>
</tr>
<tr>
<td width="30%">
<b>Qty Rejected:</b>
</td>
<td>
<asp:TextBox ID="lRejectedQty" runat="server" CausesValidation="True"></asp:TextBox>
<%-- <asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Only interger between 1 to 10000000 " ondisposed="Page_Load"
oninit="Page_Load" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>--%>
</td>
</tr>

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (lAccept.SelectedItem.Text == "Accept")
{
lRejectedQty.Enabled = false;
}
}
}

You should be able to retrieve the selected value of the dropdownlist using jQuery with something like this.
$('#<%= lAccept.ClientID %> option:selected').val();
Then you can disable your textbox with a little more jQuery with something like this.
$('#<%=lRejectedQty.ClientID %>').attr("disabled", "disabled");
And if you want to re-enable.
$('#<%=lRejectedQty.ClientID %>').removeAttr("disabled");
Hope this helps.

Related

Client Validation Function for Radio Buttons on a ASPX file

This is an ASPX/CS program with Javascript.
I have inherited this code and I am trying to complete the validation which was not completed. I have found the javascript code which validates of a radio button is clicked but the aspx code does not reference it.
Radio buttons are grouped together such that when one in the group is selected, the others are unselected. But, for starters, I cannot see how to utilize this. It must have something to do with the "GroupName" that each radio button mentions.
<ItemTemplate>
<%# FormatGroup(Eval("Group").ToString()) %>
<tr>
<td> <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'><%# Eval("Question") %></asp:Label></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion4" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion3" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion2" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion1" runat="server" GroupName='<%# Eval("Question") %>' /></td>
</tr>
</ItemTemplate>
So how would I make use of the ClientValidationFunction method in asp for radio button groups?
Try a custom field validator.
Add this bit of JavaScript:
<script language="javascript" type="text/javascript" >
function ClientValidate(source,args)
{
if(document.getElementById("<%= rblEvalQuestion1.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion2.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion3.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion4.ClientID %>").checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
Add this validator to the page:
<asp:CustomValidator id="RadioButtonValidator" runat="server" Display="Dynamic" ErrorMessage="Please select an option." ClientValidationFunction="ClientValidate" OnServerValidate="ServerValidate"></asp:CustomValidator>
And this method in the code behind:
protected void ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = rblEvalQuestion1.Checked || rblEvalQuestion2.Checked || rblEvalQuestion3.Checked || rblEvalQuestion4.Checked;
}
Finally, make sure to check if
Page.IsValid
in your button for submitting the form.
Finally, if you can re-factor this into a RadioButtonList you can simply just use a RequiredFieldValidator which will make your life a lot easier to implement!

ASP.NET Form Default Button is not working.

ASP.NET Default button and default focus is not working. My application is hosted on IIS in my server manchine. (Servername say Server1). If i am accessing the application URL outside of machine focus and default button click is working fine (http://Server1/VPath/Login.aspx) . If i am accessing the application URL using localhost in the server it is working fine (http://localhost/VPath/Login.aspx) but focus and default button click (on Enter) is not working when i am accessing the URL using server name in the server. (http://server1/VPath/Login.aspx). What could be the issue?
Note: IE version is same in all the above cases.
Here are my code files.
Login.aspx
<asp:Panel ID="LoginWrapper" runat="server" CssClass="loginWrapper" >
<asp:Login ID="LoginControl" runat="server" meta:resourcekey="LoginResource1"
DisplayRememberMe="false" OnAuthenticate="authenticate">
<LayoutTemplate>
<table>
<tr>
<td>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" meta:resourcekey="UserNameLabelResource1"
Text="User Name:"></asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server" meta:resourcekey="UserNameResource1"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="User Name is required." ToolTip="User Name is required."
meta:resourcekey="UserNameRequiredResource1" Text="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" meta:resourcekey="PasswordLabelResource1"
Text="Password:"></asp:Label></td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password" meta:resourcekey="PasswordResource1"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center; color: red">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False" meta:resourcekey="FailureTextResource1"></asp:Literal>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Login"
meta:resourcekey="LoginButtonResource1" /></td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
</asp:Panel>
Login.aspx.cs
protected void Page_Init(object sender, EventArgs e)
{
Response.RedirectLocation = Request.Url.ToString();
// bug#19156 Enter key should Login
LoginControl.Focus();
Page.Form.DefaultButton = ((Button)LoginControl.FindControl("LoginButton")).UniqueID;
}
protected void Page_Load(object sender, EventArgs e)
{
log.Debug("Loading login Page");
try
{
if (Request.QueryString["activity"] != null)
{
string activity = Request.QueryString["activity"].ToString();
switch (activity)
{
case "login":
if (Request.QueryString["Username"] != null)
un = Request.QueryString["Username"].ToString();
if (Request.QueryString["Password"] != null)
pw = Request.QueryString["Password"].ToString();
authenticate(null, null);
break;
}
}
}
catch (ExecutionEngineException ex)
{
}
if (Request.IsAuthenticated && !IsPostBack)
{
Response.Redirect("~/VPath/NotAuthorized.aspx");
}
else if (!IsPostBack)
{
HideTopMenu();
}
}
I do not know why your code is not working when accessed from outside but i think the problem
is with page_init event .
so better i would suggest to override it and this is safe too:-
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Response.RedirectLocation = Request.Url.ToString();
// bug#19156 Enter key should Login
LoginControl.Focus();
Page.Form.DefaultButton = ((Button)LoginControl.FindControl("LoginButton")).UniqueID;
}

How to get item click event in asp.net repeater control?

I am using a repeater control to show some data on my page.
The repeater item template has an image and a label field.
I want that when i click the image , I get an event containing the id field of my data item.
How can I achieve this ?
Actually when I click the image i want to go to another page and want to show detailed information of my data item, in repeater i m just showing short information.
My repeater looks like this:
<asp:Repeater ID="itemRepeater" runat="server" OnItemCreated="itemRepeater_ItemCreated" >
<ItemTemplate>
<tr>
<td colspan="2">
<asp:Image ID="phImage" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?id=" + DataBinder.Eval(Container.DataItem, "PhotoID")%>' />
</td>
<td>
<asp:Label ID="lblImageName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
I want to get PhotoID in the event when i click the image.
My photo class looks like this:
public class PhotoDC
{
public byte[] ImagebyteArray { get; set; }
public string Name { get; set; }
public int PhotoID { get; set; }
}
I have been doing winform programming just started web, maybe it is easy but i m struggling to find a solution.
I somehow managed to show hand cursor when i hover the image though.
Try this:
<asp:Repeater ID="itemRepeater" runat="server" OnItemCreated="itemRepeater_ItemCreated" >
<ItemTemplate>
<tr>
<td colspan="2">
<asp:ImageButton ID="phImage" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?id=" + DataBinder.Eval(Container.DataItem, "PhotoID")%>' OnCommand="Image_Click" CommandName="ImageClick" CommandArgument='<%# Eval("PhotoID") %>' />
</td>
<td>
<asp:Label ID="lblImageName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
protected void Image_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick"){
//e.CommandArgument --> photoid value
//Do something
}
}
You can use ItemCommand of repeater control
like this -
protected void itemRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.CommandName == "img_Click") // check command is cmd_delete
{
// get you required value
int CustomerID = Convert.ToInt32(e.CommandArgument);
//Write some code for what you need
}
}
}
Personally I think the simplest way to handle something like this is to simply use the ItemTemplate to generate a regular html link, rather than do any thing in the code-behind. Something like this:
<asp:Repeater ID="itemRepeater" runat="server" OnItemCreated="itemRepeater_ItemCreated" >
<ItemTemplate>
<tr>
<td colspan="2">
<a href="/Details.aspx?id=<%=DataBinder.Eval(Container.DataItem, "PhotoID")%>">
<asp:Image ID="phImage" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?id=" + DataBinder.Eval(Container.DataItem, "PhotoID")%>' />
</a>
</td>
<td>
<asp:Label ID="lblImageName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
</td>
</tr>
</ItemTemplate>
I know this isn't strictly the question you asked, but IMHO it's the best approach to accomplish the task.

Accessing a button in a repeater from Code Behind (C#)

I am finding some serious problems in accessing a button placed in a repeater from code behind.
This is the repeater code:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>Username:</td>
<td> <%# Eval("UserName") %></td>
</tr>
<tr>
<td>Date:</td>
<td><%# Eval("CommentTime") %></td>
</tr>
<tr>
<td>Comment:</td>
<td><%# Eval("Comment") %></td>
</tr>
<tr>
<td>
<asp:Button ID="btnDeleteComment" runat="server" Text="Delete" /></td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
And This is the code Behind placed int the page load:
Button btn = new Button();
btn = (Button)Repeater1.FindControl("btnDeleteComment");
btn.Visible = false;
Am I missing something?
Thanks
I am sure, I've solved your problem as to why you are getting the object reference not set.
As you have this conditional statement, if (e.Item.ItemType == ListItemType.Item), when its first iterates it will be a header item type. Since your your button is in the item template, that's doesn't exist in the Header template.
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Button btn = new Button();
btn = (Button)e.Item.FindControl("btnDeleteComment");
btn.Visible = false;
}
}
You can't find the control like this. What you need to do is attach an event to repeater "item data bound event" and in that event handler do:
(Button)e.Item.FindControl("btnDeleteComment");
on .aspx
<asp:Button ID="btnDeleteComment" runat="server" Text="Delete" Visible='<# IsAuthor?"true":"false" >' />
in code behind
//global scope
public bool IsAuthor=false;
//in pageload event
IsAuthor= GetIsAuthor();

ListView fields not getting posted

I know I've done something like this before, but I have no idea why it isn't working now. I have a ListView with some textboxes. I want to read the text out of those boxes when I click a button (linkbutton, whatever).
<asp:ListView runat="server" ID="lv_bar" EnableViewState="true">
<LayoutTemplate>
<table>
<tr>
<th>Foo</th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:LinkButton ID="lb_delete" CausesValidation="false" runat="server" Text="Del" /></td>
<td><asp:TextBox id="txt_foo" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:LinkButton ID="lb_add" CausesValidation="false" runat="server" Text="Add" />
And then here's the relevant code-behind stuff:
protected void Page_Load(object sender, EventArgs e)
{
lb_chapter_add.Click += lb_chapter_add_Click;
if (!IsPostBack)
{
lv_chapters.DataSource = new List<Foo>() { new Foo() { Name = "harbl"} };
lv_chapters.DataBind();
}
}
void lb_add_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lv_bar.Items)
{
var txt_foo = (TextBox)item.FindControl("txt_foo");
Response.Write("foo: " + txt_foo.Text);
}
Response.Write("<br />the end");
Response.End();
}
But what I see when I enter some text into txt_foo and click lb_add is just "the end". What am I doing wrong here?
The problem it that you are using a a non persistent object as DataSource.
Due to clicking the button, you generate a postback and lv_chapters does not contain any items. Set a breakpoint in the line where the foreach is and you will see that lv_chapters.Items in null, or that it's Count property returns 0.

Resources