. NET 2.0 DETAILSVIEW Hidden TextBox

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!
RSSIAdminAsked:
Who is Participating?
 
GavinMannionConnect With a Mentor Commented:
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
 
GavinMannionCommented:
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
 
Elvio Lujan.Net Senior DeveloperCommented:
<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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RSSIAdminAuthor Commented:
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
 
GavinMannionCommented:
:), 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
 
RSSIAdminAuthor Commented:
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
 
GavinMannionCommented:
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
 
RSSIAdminAuthor Commented:
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
 
GavinMannionCommented:
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
 
RSSIAdminAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.