Category Archives: ScriptManager

[RESOLVED] changing backcolor of textbox when input is invalid......

Hello I have an ASP.net page with two textboxes where the user types in input.  I applied a requiredfieldvalidator to both textboxes as well.  However, I also want the textboxes to change to a backcolor of red if the form is invalid when the user clicks the button to submit the input.  I tried this code below in tthe button click event, but doesnt work...


If Page.IsValid = False Then
            txtCosts.BackColor = Drawing.Color.Red
            txtRevenue.BackColor = Drawing.Color.Red
        End If

How else can I change the Backcolor of the textboxes when the input is invalid.......

hi bababooey check this link here i explain clearly how to change the textbox background colorwhenever validation fails using asp.net

http://aspdotnet-suresh.blogspot.com/2011/01/how-to-change-textbox-background-color.html

You can do such validations client side.

Ofcourse the page needs to validated on the server too.

Here's an example  

//aspx
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script language="javascript" type="text/javascript">
        function abc(){
            if (typeof (Page_ClientValidate) == 'function') {
            Page_ClientValidate();
            }
            ChangeBackColor();
            if (!Page_IsValid) {
                return false;
            }
            return true;
        }


        function ChangeBackColor() {
            for (var i = 0; i < Page_Validators.length; i++) {
            var val = Page_Validators[i];
            var ctrl = document.getElementById(val.controltovalidate);
            if (ctrl != null && ctrl.style != null) {
                if (!CheckIfValid(ctrl)) {
                    ctrl.style.background = 'red';
            }
            else {
                ctrl.style.background = '';
            }
            ValidatorUpdateIsValid();
            ValidationSummaryOnSubmit(val.validationGroup);
        }


        function CheckIfValid(control) {
            for (var i = 0; i < control.Validators.length; i++) {
                if (!control.Validators[i].isvalid) {
                    return false;
                }
            }
            return true;
        }
    }
}
    </script>
    <div>
      <asp:TextBox ID="TextBox1" runat="server"  ></asp:TextBox>
      <asp:RequiredFieldValidator ID="Req1" runat="server" ErrorMessage="Required" Display="Dynamic"  ControlToValidate="TextBox1" ForeColor="Red"  Text="text1 Required" ValidationGroup="VGrp1"></asp:RequiredFieldValidator>
      <br />
      <asp:Button ID="btnsubmit"  runat="server" Text="Submit" OnClientClick="abc();"  onclick="btnsubmit_Click"/>
    </div>
    </form>


//codebehind
protected void btnsubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("VGrp1");
        if (!Page.IsValid)
        {
            return;
        }
    }


 

If you are interested learning more on client side validation

http://simpledotnetsolutions.wordpress.com/2011/02/06/the-nuts-bolts-of-client-side-validation/

[RESOLVED] display real time data in label using MS acccess database

Hi,

 

I need to display the real time data in the label.

Explain In detail: 

The camera is counting the people and store the counted value in database every minute.

so i need to pick the data and display it same on the screen. There are lot of cameras counting. So i need to take camera number and display respective count.

 

Camera NUmber Counted Value
1 Label1(Display realtime data here)
2 Label2

Can anybody advice me how and which way i can achieve this

Thanks in advance

If you want to do it in WebApp. Do it with AJAX controls. Add a ScriptManager control on top, add update panel and labels in it to update from database. Add a AJAX timer control for continuous update at specified interval.

Hi,

Thanks for your reply

I did the same its shows an error : the name 'label1' doesnt exists in current context

I explain my code here:

<div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <asp:ScriptManager ID="ScriptManager1" runat="server"></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        </asp:ScriptManager> </div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditionals></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        <ContentTemplate></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            <asp:Timer ID="Timer1" runat="server" Interval="1000"></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            </asp:Timer></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        </ContentTemplate></div> <div style="position: absolute; left: -10000px; top: 26px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        </asp:UpdatePanel></div>

 <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager> 

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditionals>

        <ContentTemplate>

            <asp:Timer ID="Timer1" runat="server" Interval="1000" onTick="Timer_Click">

            </asp:Timer>

            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

        </ContentTemplate>

        </asp:UpdatePanel>


I check all way but still its through the same error. I'm using multiple UPDATEPANEL.

I'm using  15 updatepanel along with the timer to refresh and label to display data. But at .cs file i have used 

public void Timer_Click(object sender, EventArgs e)

{

  Dbcommection_Method();

Label1.Text = data ;

}


HERE it throughs "Label1" doesnt contain in current context


Thanks a lot

I think this is a Designer issue....delete the label control and re-add to the page....if still problem persists, then try by creating a new page and add controls one by one....

Thanks.

[RESOLVED] ImageButton OnClick cause - Invalid postback or callback argument

I have a page that uses a master page. The following code only shows the bare minimum of the page:


<asp:ListView ID="lvGalleries" runat="server">
  <LayoutTemplate>
    <table>
      <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td>
        <asp:ImageButton OnClick="LoadGallery_Click" runat="server" ImageUrl='<%# Eval("Url") %>'  />
      </td>
    </tr>
  </ItemTemplate>
  <EmptyDataTemplate>
    <tr><td>none</td></tr>
  </EmptyDataTemplate>
</asp:ListView>




Code-behind:

protected void LoadGallery_Click(object sender, ImageClickEventArgs e) 
    {         
       //code
    }


The

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration 
or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback 
or callback events originate from the server control that originally rendered them.  
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or 
callback data for validation.




My guess is that it may releated to the ID's that are recreated..

Change the enableEventValidation="true" in the page directive at the top of your aspx page to 'false'

 

You have to add ListView.Databind() in the Page_Load . That will take care of the expression in the imageUrl. Hope this helps

I have DataBind() in the Page_Load.

The change of enableEventValidation="true" to false works.
But the event isnt fires / I dont get to the breakpoint at the beginning of the function. 

And what does enableEventValidation="false" cause?

can you share the code with which you are binding the listview control

Did you saw my previous reply edit?

Its nested code, that execute a select steatment and return datatable. That datatable I bind to the datasource of the listview, then I do databind...

Any thoughts?

are you doing Databinding in IsPostBack Check???? that might be one of the reason for making the event validation corrupted....do databind in all page_load...try like that....

Thanks.

protected void LoadGallery_Click(object sender, ImageClickEventArgs e)
    {
      //code
 }


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




    private void RefreshGalleries()
    {
        lvGalleries.DataSource = // call to a function that returns datatable
        lvGalleries.DataBind();
    }

this is the code i use

Can you include the code RefreshGalleries(); in If(!IsPostBack) and check what is happening....

I seriously doubt that binding everytime the listview in page_load might cause the problem....

Thanks.

Well, nothing happens. The LoadGallery_Click still isnt firing...

When you click on the link, Does it postback to the server or open the link without a postback?

<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; margin: 8px;" mce_style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; margin: 8px;">

I see the page reload - its meen there been a postback, I put breakpoint on the LoadGalleries_Click and it didnt get there.

The ImageButton doesnt have navigateUrl as you can see so theres no link to open.

</div>

I developed a simple example w/ ListView & imageButton ,

just very similar , what you have. This works perfect & I don't see any error.

Here it is

//aspx
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ListView ID="lvGalleries" runat="server">
  <LayoutTemplate>
    <table>
      <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td>
        <asp:ImageButton ID="ImageButton1" OnClick="LoadGallery_Click" runat="server" ImageUrl='<%# Eval("URL") %>'  />
      </td>
    </tr>
  </ItemTemplate>
  <EmptyDataTemplate>
    <tr><td>none</td></tr>
  </EmptyDataTemplate>
</asp:ListView>
</asp:Content>




//codebehind
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lvGalleries.DataSource = GetDT();
            lvGalleries.DataBind();
        }
    }


    private DataTable GetDT()
    {
        DataTable dtDFS = new DataTable();
        dtDFS.Columns.Add("ID");
        dtDFS.Columns.Add("URL");
        dtDFS.Columns.Add("Name");


        DataRow dr = dtDFS.NewRow();
        dr["ID"] = 1;
        dr["URL"] = "www.google.com";
        dr["Name"] = "Google";
        dtDFS.Rows.Add(dr);




        dr = dtDFS.NewRow();
        dr["ID"] = 2;
        dr["URL"] = "www.microsoft.com";
        dr["Name"] = "MS";
        dtDFS.Rows.Add(dr);


        dr = dtDFS.NewRow();
        dr["ID"] = 3;
        dr["URL"] = "www.yahoo.com";
        dr["Name"] = "yahoo";
        dtDFS.Rows.Add(dr);
        return dtDFS;
    }
    protected void LoadGallery_Click(object sender, ImageClickEventArgs e)
    {
        int r1 = 9;
    }


 

You got me wonder how this possible, so I dug into my code and saw that all this time i was click the wrong link (this page isnt design yet).

Thanks a lot and sorry for the HUGE mix up.

[RESOLVED] How to activate javascript function from Page_Load and updatepanel

Hi..

In the first Page_Load, I am activating a javascript function to make an ImageButton change between 2 images with an interval.

This works fine.

 

Now when pressing this imagebutton, the Page_Load event will fire the second time and all this is happening in an updatepanel which means a partial postback.

 

The question is how to execute the javascript function stopbk() in the Page_Load event in this scenario as I cant just put it in the "Onload". How can I make that work?

Code is below:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SecondLoad"] != null)
        {
            //How to activate stopbk() ?
            Label1.Text = "Second Load";
        }


        if (Session["SecondLoad"] == null)
        {
            Label1.Text = "First Load";
            Session["SecondLoad"] = "dummy";
            body1.Attributes.Add("Onload", "startbk()");
        }
    }
    protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
    {
        //
    }






<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>




<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<script type="text/javascript">
    var TimerProps = {
        refTimer: null,
        index: 0
    }
    function startTimer() {
        if (TimerProps.index > 1) {
            TimerProps.index = 0;
            document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/inboxKuvert.jpg";
            TimerProps.index += 1;
        }
        else {
            document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/Transparent.gif";
            TimerProps.index += 1;
        }
    }
    function startbk() {
        TimerProps.refTimer = setInterval("startTimer()", 550)
    }


    function stopbk() {
        clearInterval(TimerProps.refTimer)
    }
</script>


<asp:ImageButton ID="ImageButton6" runat="server" Height="12px" Width="16px"
 ImageUrl="http://www.abc.com/images/inboxKuvert.jpg" onclick="ImageButton6_Click" />


    <br />
    <asp:Label ID="Label1" runat="server" Text="-"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>


 

ASPusers

The question is how to execute the javascript function stopbk() in the Page_Load event in this scenari

ScriptManager.RegisterStartupScript()....

http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerstartupscript.aspx

Thanks.

Thank you,

I have tried to understand the example but are not 100% sure of how the parameters should be set.

My attempt is as below but are not sure if I have done it correctly, I think I miss something?

Example:

RegisterStartupScript( Control control, Type type, string key, string script, bool addScriptTags );

 

My attempt:
RegisterStartupScript(ImageButton6, GetType(), "???", "stopbk()", true); 

It seems to work with the example below:

Thank you!

 

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SecondLoad"] != null)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "stopbk", "stopbk();", true);
            Label1.Text = "Second Load";
        }


        if (Session["SecondLoad"] == null)
        {
            Label1.Text = "First Load";
            Session["SecondLoad"] = "dummy";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "startbk", "startbk();", true);
        }
    }
    protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
    {
        //
    }




<script type="text/javascript">
    var TimerProps = {
        refTimer: null,
        index: 0
    }


    function startTimer() {
        if (TimerProps.index > 1) {
            TimerProps.index = 0;
            document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/inboxKuvert.jpg";
            TimerProps.index += 1;
        }
        else {
            document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/Transparent.gif";
            TimerProps.index += 1;
        }
    }
    function startbk() {
        TimerProps.refTimer = setInterval("startTimer()", 550)
    }
    function stopbk() {
        clearInterval(TimerProps.refTimer)
    }
    </script>


 

[RESOLVED] Resource name '<name>.Resources.resources' cannot be used more than once.

Hi All

I have recently upgraded to VS 2010 and converted my web site into a web application. Now, when I attempt to build, I receive the error:

Resource name '<name>.Resources.resources' cannot be used more than once.

There is no page name or anything, and I have completely exhausted all that Google has to offer.

Can someone help please?

Thanks

Ben

Hi,

Not very sure about the issue. Do you have any Resources file or ScriptManager, AJAX control in your web application?

Maybe you can try to find out all the resource file and then add them. Then complie application again.

This article described this error:

http://www.punkcoder.com/2005/02/nant-error-cs1508-bc31502-resource-has.html

And here's a similar thread, you can check it:

http://forums.asp.net/t/1306881.aspx

Hope this can help you.

Hi, thanks for getting back to me.

I have actually just figured this out and thought I'd share it in case it helps anyone in the future.

To fix the issue I browsed to [obj > Debug] and [obj > release] and deleted any files which referenced the word resources. After this my project built successfully.

Hope that helps someone!

Ben