?
Solved

. NET 2.0 DETAILSVIEW Hidden TextBox

Posted on 2006-05-11
10
Medium Priority
?
2,835 Views
Last Modified: 2008-01-09
Hi Expert:

Is it possible to have a hidden textbox in a DetailsView TemplateField that can be set in the
code behind by some other device or event .

If so, how would you make reference( point to hidden textbox) and write to the hidden textbox in the codebehind.

Here is a portion of my code:

    <asp:DetailsView
        DataSourceID="PersonSource"
        AutoGenerateRows="false"
        DefaultMode="Insert"
        CellPadding="5"
        HeaderText="Person"
        Runat="Server">
        <Fields>
            <asp:TemplateField HeaderText="You cannot see me"  Hidden >
            <EditItemTemplate>
                <asp:TextBox  
                    id="hiddentxt"
                    Text='<%#Bind("you need to click on of the above checkboxes ")%>'
                    Runat="Server" />
                <asp:RequiredFieldValidator
                    ValidationGroup="person"
                    ControlToValidate="hiddentxt"
                    Text="(required)"
                    Runat="Server" />
            </EditItemTemplate>
            </asp:TemplateField>

Thank you expert!
0
Comment
Question by:RSSIAdmin
  • 5
  • 4
10 Comments
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16658029
I would presume it would act like a normal textbox and you can access it the same way you access any control on a DetailsView.

Why would you want a hidden textbox though, if you don't need to access it using javascript then just store the value in the back end.
0
 
LVL 18

Expert Comment

by:Elvio Lujan
ID: 16658119
<asp:DetailsView
        DataSourceID="PersonSource"
        AutoGenerateRows="false"
        DefaultMode="Insert"
        CellPadding="5"
        HeaderText="Person"
        Runat="Server">
        <Fields>
            <asp:TemplateField HeaderText="You cannot see me"  Hidden >
            <EditItemTemplate>
                <asp:TextBox  width='0px'
                    id="hiddentxt"
                    Text='<%#Bind("you need to click on of the above checkboxes ")%>'
                    Runat="Server" />
                <asp:RequiredFieldValidator
                    ValidationGroup="person"
                    ControlToValidate="hiddentxt"
                    Text="(required)"
                    Runat="Server" />
            </EditItemTemplate>
            </asp:TemplateField>
0
 

Author Comment

by:RSSIAdmin
ID: 16659957
GavinMannion and lem2802  thanks for the quick response and welcomed advise.

The problem is I have a detailsview control (set for INSERT mode) which  validates the textboxes using <asp:RequiredFieldValidator
                    ValidationGroup="person"
                    ControlToValidate="hiddentxt"
                    Text="(required)"
                    Runat="Server" />
(ValidationGroup="person"

This DetailsView INSERTS the records ok and the textbox validation works fine!

However I need to add  2 radio buttons and 5 checkboxes ( or a checkboxlist horizonal aligned) to the DetailsView which will  output a "1" from each checkbox that is checked and
a "1" from the 2 radiobutton ( on or off). A database field is assigned to each checkbox output and
the output is "1"fselected. Same goes for the radiobuttons.

Bottom line is I don't know how to validate that the user in fact checked any of the checkboxes and both radiobuttons and  how to get the checkbox(s) output  "1" if checked  ( same goes for the radiobuttons) back to the database similar like I did for  the textbox inputs Text='<%Bind("last_name") .

I was thinking this could be done by having the respective checkboxes and radiobuttons set a "1" in the DetailsView respective hidden textfields resulting in complete validation and
outputs to the database via the hidden textboxes.

I guess I am a real hacker!

help!




0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:GavinMannion
ID: 16660986
:), I know the feeling of trying to hack around something.

Personally I would recommend changing your insert statement to work through the back end and then validating in the codebehind before inserting.

Hidden fields are very easy to fake, so try not to use them unless it's not really important.

0
 

Author Comment

by:RSSIAdmin
ID: 16662322
Expert Gavin: Your right but I need something fast and application will be used for a very short time ( every appears ok except noted above ( a miss is as far as a mile!)

The  DetailsView CheckBoxField's look like this,  5 times   car2 ,car3.. etc car5
<asp: CheckBoxField DataField="car1" HeaderText="CARS" Text="car1"/>

puts a "1" in car1 data if CheckBoxField is checked ( if not a "0") works fine (but no validation)
all checkboxfields and radioboxes default uncheck when the page first comes up!

ACTION HELP

please show me how to add a  "off-on" radiobutton ( output "1" if selected "On" or "0" off) to
my DetailsView INSERT control  Would it look like this????

<asp: RadioButton DataField="truck1" HeaderText="TRUCKS" Text="track1"/>

2) PLease kindly show me how to use javascript to scan the 5 CheckBoxField's and 2 RadioButton's to check if any of  one of the 5 CheckBoxFields are check and same for RadioButtons and hold up INSERT action.

I woulf be happy if you can help here and thanks to lem2802 for input on this matter...

0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16665497
The javascript way could be a bit complicated, if you really need it ask in the Javascript TA before I send you down the wrong path.

From the codebehind page can you not put this into your onClick event of your button.

int CheckBoxCount = 0;

foreach( CheckBox cb in DetailsView)
{
     if( cb.Checked)
     {
        CheckBoxCount++;
     }
}

if( CheckBoxCount > 0 )
{
      //Submit to database
}
else
{
      //Display error message
}
0
 

Author Comment

by:RSSIAdmin
ID: 16668297
Expert+ GavinMannion:  Thanks  (still in big trouble here)

1)  Help, I  do not know how to implement the "onClick event " which I am assuming has to be added to the "ShowInsertButton" and get your code (hooks) above to stop insert of record.

Does the "ShowInsertButton" some how "validate false" and block the  insert from happening?

2) In my code behind how do I references  my "DetailsView1" and chbox1,chbox2.

In my code behind I get "error chbox1 not in Content???   used DetailsView1.chbox1

foreach( CheckBox?? cb in DetailsView??)
{
     if( cb.Checked)
     {
        CheckBoxCount++;
     }
}

if( CheckBoxCount > 0 )
{
      //Submit to database
}

 My code looks like below

<asp:DetailsView
        DataSourceID="PersonSource"
        ID="DetailsView1"
        AutoGenerateRows="false"
        DefaultMode="Insert"
        CellPadding="5"
        HeaderText="Person"
        Runat="Server">
        <Fields>
            <asp:TemplateField HeaderText="First Name" >
            <EditItemTemplate>
                <asp:TextBox   id="firstnametxt"
                    Text='<%#Bind("firstname")%>'
                     Runat="Server" />
                <asp:RequiredFieldValidator
                    ValidationGroup="person"
                    ControlToValidate="firstname"
                    Text="(required)"
                    Runat="Server" />
            </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText=" chbox2" >
            <EditItemTemplate>
        <asp:CheckBox id="ckbox2" Runat="Server" />
        </EditItemTemplate>
            </asp:TemplateField>

           etc for the other 3 checkkboxes

        <asp:CommandField ShowInsertButton="True"
        InsertText="Add Person" />
      </Fields>
     </asp:DetailsView>

<asp:SqlDataSource ID="PersonSource" runat="server" ConnectionString="<%$ ConnectionStrings:Person %>"

   InsertCommand="INSERT INTO persons firstname,checkbox1Column,checkbox2Column, etc) VALUES (@firstname, @checkbox1value,@checkbox2value, etc...)" >

                 if checkbox1 is clicked  if checkbox2 is clicked
                 checkbox1value =1      checkbox1value =1   otherwise "0"

                 checkbox1value =1
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16672481
Unfortunately I don't have a dev environment at hand at the moment to give you the exact syntax.

However you need to change your button from an insert button to a command button, then intercept the command and if everything validates then call the PersonSource.Insert() command manually.

Basically imagine your insert button has nothing at all to do with the details view.

Also try

CheckBox cb1 = (CheckBox)DetailsView.FindControl("CheckBox1");
0
 

Author Comment

by:RSSIAdmin
ID: 16682120
Expert++  GavinMannion ....
Again, thank you!! but need your help

1) I did create a button with "on click" action that calls:
public void updateheckboxstatus(Object sender,Events Aargs e) {
CheckBox cb1 = (CheckBox)DetailsView1.FindControl("CheckBox1");
CheckBox cb2 = (CheckBox)DetailsView1.FindControl("CheckBox2");
      ..       cb3 = etc
if(cb1.Checked || cb2.Checked || cb3.Checked || ....)
Label.Text= " checkbox was checked!!);

2) Now the hard part that I kindly need from you is how to replace the  <asp:CommandField ShowInsertButton="True"  InsertText="Add Person" />  button with the new on click button which should insert records when clicked simliar to the old DetailsViews default Insert button without moving/changing the InsertComand="Insert into ....... and all that good free stuff .

3) With regard to preventing "record insert"  if none of the checksboxes are checked.  I am thinking I can set the new Insert button to vaildate and return false will stop insert.

Help!


0
 
LVL 15

Accepted Solution

by:
GavinMannion earned 2000 total points
ID: 16688319
I am presuming the code you have in 1) works for you?

I would avoid using the <asp:CommandField in this regard rather just use a normal button and call the insert command yourself from the code...., else you can look for the event DetailsView_Command or something like that and then place your code from 1) inside that.

3) this is why I would avoid the CommandField and rather use a normal button that gives you full control, else in the DetailsView_Command event you can validate and stop the insert there.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question