Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2004, 09:26 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I want to go on this thread because I have the same problem badgolfer told
now...
How can I make my contorols(server-side or client-side) in a DataGrid object
(in a Template Column) and allocate them a unique ID according to a integer column in my database and for each control in my DataGrid I could have
an OnClick attribute executes a script(server-side or client-side) that this script shows the ID of the controls clicked
my problem here is I can't bind my datasource to ID attribute but for example
it works fine for Text attribute
--------------------------------------
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" EnableViewState="False">
                <Columns>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:Button ID='<%# DataBinder.Eval(Container.DataItem, "EmployeeID","{0:c}") %>' Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeID") %>' BackColor="#f1f1f1" Runat=server >
//here Parser error occures
//Parser Error Message: '<%# DataBinder.Eval
//(Container.DataItem, "EmployeeID","{0:c}") %>' is not a valid identifier.

                            </asp:Button>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
--------------------------------------------
in my code I want to repeat a button and allocate them unique ID and then when user clicks on evry button a simple message could show me the ID of the control clicked.
Any help appreciated.


--------------------------------------------
Mehdi
I'm waiting for your better idea.
 
Old July 24th, 2004, 12:03 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Usually, you assign the ID for a button in a template something static: cmdMyButton. Because the control is inside a repeating template, .NET automatically creates a unique ID based on the control's parent container. The HTMl for the button ends up looking something like this:

<input type="submit" name="grdMyGrid$_ctl3$cmdMyButton" ...

The next datagrid row will contain this button:

<input type="submit" name="grdMyGrid$_ctl4$cmdMyButton" ...

In this case, _ctl3 and _ctl4 are the row controls and cmdMyButton exists uniquely as a single child of each.

You also then need to specify a server method to call in the control:

<asp:button id="cmdMyButton" runat="server" onclick="cmdMyButton_OnClick" />
 
Old July 24th, 2004, 05:21 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thank you for your answer
I think if I use ClientID property in my code(server-side) could be much better ....
I think it is very easy to achieve my goal while using both server-side controls and server-side scripts but here I want to use server-side controls with client-side scripts that means I want to obtain the ID's of my server-side controls through the client-side scripts and then for example change the backcolor of my controls using javascript ....
for example I want to change the backcolor of my the server-side control when OnFocuse event occurs according to it's ID in a client-side script not in a server-side script.
I think here I should use javascript ...but I don't know javascript at all
Thanks and Regards.

--------------------------------------------
Mehdi
I'm waiting for your better idea.
 
Old July 24th, 2004, 09:17 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Something like that is actually much much easier.

You can write a javascript function that sets the style properties or changes the css class of a control. You call this function from the control's onFocus and onBlur events. When you call the function, you can just pass it "this" and the function will use that to do what it needs, and does not need to deal with the clientID of the server controls.
<script language="JavaScript">
function textbox_onFocus(objTextBox){
    //do stuff here with objTextBox
}
function textbox_onBlur(objTextBox){
    //do stuff here with objTextBox
}
</script>

Textbox control attributes:
onFocus="textbox_onFocus(this);"
onBlur="textbox_onBlur(this);"

You may need to set these attributes in the codebehind by using some of the datagrid events (because sometimes certain attribute assignments in the markup of a server control get ignored or overwritten by .NET). But certainly try them right in the markup first to see if they carry thru. I'm pretty sure that if you put 'onFocus="textbox_onFocus(this);"' inside the <asp:button ...> tag it will carry thru to the HTML output.

If you have specific javascript questions, try the javascript category on this forum.
 
Old July 25th, 2004, 03:43 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thank you Peter for your complete answer
now!!!
Peter,consider that I have a client-side control and I want to change the properties of it using server-side sciripts.Is it Possible at all?
Can I give an attribute(event) of a client control,server-side scripts...
Thanks in advance.

--------------------------------------------
Mehdi
I'm waiting for your better idea.
 
Old July 25th, 2004, 08:43 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well, consider that all controls are client-side. While some are both client and server-side.

Lets say you want a client-side button, just a plain HTML button:

<input type="button" ...>

Normally, when you put that on a web form it's literal text/html and can not be accessed by server code. So instead you do this:

<input type="button" id="btnMyButton" runat="server" ...>

This makes it into a server control of type System.Web.UI.HtmlControls.HtmlInputButton. Then you give it a local instance in the code-behind:

Protected btnMyButton As System.Web.UI.HtmlControls.HtmlInputButton

Now you can access it from the server-side code to modify its properties:

btnMyButton.Attributes.Item("onClick") = "alert('You clicked my button!');"

And so on.

Regarding your second question: No. You can not. A client-side control is a client-side control. To put it simply: If you want a control to fire a server-side event, then you need a server control.
 
Old July 26th, 2004, 03:04 AM
Friend of Wrox
 
Join Date: Sep 2003
Location: Whitstable, , United Kingdom.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Thanks to all above for the discussion and full
answers. I have my problem solved now and can fire
off both server-side and client-side activity.

I have also bought a simple HTML beginners book which
has also helped clear up a few details.

Cheers again.

Chas (Canterbury, UK)


 
Old July 26th, 2004, 03:21 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks to Peter and the book badgolfer bought :D
Peter,I completely got the answer of my second question but in relation to my first question....
I don't want to convert my HTML control to a server-side control
>>Normally, when you put that(client-side HTML control) on a web form it's >>literal text/html and can not be accessed by server code
>>
why?
we can use LiteralControl for accessing client-side HTML controls in our ASP.NET pages but I don't know how,for example
-----------------------------
foreach(LiteralControl lit in Page.Controls)
{
Label1.Text+=lit.Text+" , ";
}
-----------------------------
but in the above code there is a runtime error "Specified cast is not valid."
I think in Page.Controls evry object is as type of LiteralControl because when
I test it,the GetType method for evry control in Page.Controls confirms that evry control in my Page.Controls is as type of LiteralControl but here I can't get why this cast error occures(in my page I have also a datagrid and some simple html controls)
Can you help me use LiteralControls correctly.
Thanks in advance.


--------------------------------------------
Mehdi
I'm waiting for your better idea.
 
Old July 26th, 2004, 05:06 AM
Friend of Wrox
 
Join Date: Sep 2003
Location: Whitstable, , United Kingdom.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default


A further point of syntax...

I want a client-side 'onclick' event to set a variable value e.g.
my HTML may look like this:

<script
  var sString; // a simple string variable - could be up to 200 chars
</script>

then in a column definition...
<asp:TemplateColumn HeaderText="Detail">
<ItemTemplate>
<Button ID="Button1"
onclick="sString='<%#DataBinder.Eval(Container.Dat aItem,"field1")>';"
>Button Caption
</Button>
</ItemTemplate>
</asp:TemplateColumn>

the line containing the 'onclick' HTML to set variable sString to
the text value of database field 'field1' is the problem. When
I go back from the HTML pane to the design pane it tells me
"Cannot open design view. Quote values differently inside a
<% "value" %> block. Why is my onclick syntax wrong for setting
variable sString. (NB. sString is a variable that I will use later
depending on another button push). I have tried various combinations of syntax.

TIA.

 
Old July 26th, 2004, 10:14 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You are missing the closing %

<Button ID="Button1"
onclick="sString='<%#DataBinder.Eval(Container.Dat aItem,"field1")%>';"
>Button Caption
</Button>




Similar Threads
Thread Thread Starter Forum Replies Last Post
datagrid command buttons CyberW ASP.NET 3.5 Basics 21 November 29th, 2008 04:02 PM
Using command buttons in Access kelljw Access VBA 10 July 5th, 2006 12:42 PM
Using command buttons in Access kelljw BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 4th, 2006 07:21 AM
Labels as command buttons RichMW Access VBA 0 February 18th, 2005 09:10 AM
Graphics For Command Buttons Teqlump Access 1 November 24th, 2004 11:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.