IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

[C#] comment remplir un champ de formulaire par une variable...


Sujet :

ASP.NET

  1. #1
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Points : 62
    Points
    62
    Par défaut [C#] comment remplir un champ de formulaire par une variable...
    Bonjour,

    Suite des mes nombreuses questions sur le developement web .net.

    Sur ma page, une formview affiche le contenu d'une table (la ligne la plus récente).

    Voici le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    <%@ Page Language="C#" MasterPageFile="~/MasterPage_admin.master" Title="Untitled Page" %>
     
    <script runat="server">
     
        protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {
     
        }
     
        protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
        {
     
        }
    </script>
     
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
       <div style="text-align:left">
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="idinfo" DataSourceID="SqlDataSource1"
            Height="163px" OnPageIndexChanging="FormView1_PageIndexChanging1" Width="600px" AllowPaging="True">
            <EditItemTemplate>
                idinfo:
                <asp:Label ID="idinfoLabel1" runat="server" Text='<%# Eval("idinfo") %>'></asp:Label><br /> 
                date:
                <asp:TextBox ID="dateTextBox" runat="server" Text='<%# Bind("date") %>'>
                </asp:TextBox><br />
                titre:
                <asp:TextBox ID="titreTextBox" runat="server" Text='<%# Bind("titre") %>'>
                </asp:TextBox><br />
                Texte :
                <asp:TextBox ID="infoTextBox" runat="server" Text='<%# Bind("info") %>' Height="153px" Width="563px" TextMode="MultiLine" MaxLength="250">
                </asp:TextBox><br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Mettre à jour">
                </asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Annuler">
                </asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                date:
                <asp:TextBox ID="dateTextBox" runat="server" Text='<%# Bind("date") %>'>
                </asp:TextBox><br />
                Titre:
                <asp:TextBox ID="titreTextBox" runat="server" Text='<%# Bind("titre") %>' Height="18px" Width="519px"></asp:TextBox><br />
                <br />
                Votre texte :<br />
                <asp:TextBox ID="infoTextBox" runat="server" Text='<%# Bind("info") %>' Height="153px" Width="563px" TextMode="MultiLine" MaxLength="250"></asp:TextBox><br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insérer">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Annuler">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
                Edito numéro :
                <asp:Label ID="idinfoLabel" runat="server" Text='<%# Eval("idinfo") %>'></asp:Label> Publié
                le:
                <asp:Label ID="dateLabel" runat="server" Text='<%# Bind("date") %>'></asp:Label><br /><br />
                Titre:
                <asp:Label ID="titreLabel" runat="server" Text='<%# Bind("titre") %>'></asp:Label><br /><br />
                Texte:
                <asp:Label ID="infoLabel" runat="server" Text='<%# Bind("info") %>'></asp:Label><br /><br />
                <div class="bouton_modif">
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Modifier"></asp:LinkButton>
                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Supprimer"></asp:LinkButton>
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                    Text="Nouveau"></asp:LinkButton></div>
            </ItemTemplate>
        </asp:FormView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:intranetConnectionString %>"
            DeleteCommand="DELETE FROM [infoaccueil] WHERE [idinfo] = @idinfo" InsertCommand="INSERT INTO [infoaccueil] ([date], [titre], [info]) VALUES (@date, @titre, @info)"
            SelectCommand="SELECT * FROM [infoaccueil] ORDER BY [idinfo] DESC" UpdateCommand="UPDATE [infoaccueil] SET [date] = @date, [titre] = @titre, [info] = @info WHERE [idinfo] = @idinfo">
            <DeleteParameters>
                <asp:Parameter Name="idinfo" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="date" Type="String" />
                <asp:Parameter Name="titre" Type="String" />
                <asp:Parameter Name="info" Type="String" />
                <asp:Parameter Name="idinfo" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="idinfo" Type="Int32" />
                <asp:Parameter Name="date" Type="String" />
                <asp:Parameter Name="titre" Type="String" />
                <asp:Parameter Name="info" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        </div>
    </asp:Content>

    J'aimerais codé le test suivant:

    Je compte le nombre de résultat du Select *.... si la valeur est 0 (rien dans la table), j'affiche un message, sinon, j'affiche la FormView comme c'est le cas actuellement.

    Je ne sais pas du tout comment m'y prendre (sauf pour le code SQL à faire). Merci pour votre aide.


  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    581
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2004
    Messages : 581
    Points : 633
    Points
    633
    Par défaut
    au démarrage de ta page ?

    Ben en faite tu as deux parties de code en .net (html en partie server en C# ou vb)

    Dans la partie server, tu as un page_load, il te suffit de faire ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     
    if(!isPostback)
    //Ta reqête Sql , via SqlConnection, SqlCommand et Execute blablba
    Tu as différentes méthodes dont une qui peut te renvoyer un nombre de tuples reçu après exécution de ta requête et donc si c'est = 0 tu fais soit un Response.Redirect(AutrePage avec message) ou une alert javascript, sinon tu continues.

    Fix

  3. #3
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Points : 62
    Points
    62
    Par défaut
    Citation Envoyé par fix105
    au démarrage de ta page ?

    Ben en faite tu as deux parties de code en .net (html en partie server en C# ou vb)

    Dans la partie server, tu as un page_load, il te suffit de faire ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     
    if(!isPostback)
    //Ta reqête Sql , via SqlConnection, SqlCommand et Execute blablba
    Tu as différentes méthodes dont une qui peut te renvoyer un nombre de tuples reçu après exécution de ta requête et donc si c'est = 0 tu fais soit un Response.Redirect(AutrePage avec message) ou une alert javascript, sinon tu continues.

    Fix

    Je vois pas du tout où est cette partie page_load.

    Cette instruction par exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     String date_now = DateTime.Now.ToLongDateString();
    Console.Out.WriteLine(date_now);
    Je la mettrais aussi dans page load?

    Désolé je suis perdu par rapport aux pages html avec du php

  4. #4
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Points : 62
    Points
    62
    Par défaut
    je reprend en fait mon problème :


    voici le code de ma page (de nouveau) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    <%@ Page Language="C#" MasterPageFile="~/MasterPage_admin.master" Title="Untitled Page" %>
    
    <script runat="server">
    
    
    
        protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
        {
    
        }
    </script>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
       <div style="text-align:left">
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="idinfo" DataSourceID="SqlDataSource1"
            Height="163px" OnPageIndexChanging="FormView1_PageIndexChanging1" Width="600px" AllowPaging="True">
            <EditItemTemplate>
                idinfo:
                <asp:Label ID="idinfoLabel1" runat="server" Text='<%# Eval("idinfo") %>'></asp:Label><br /> 
                date:
                <asp:TextBox ID="dateTextBox" runat="server" Text='<%# Bind("date") %>'>
                </asp:TextBox><br />
                titre:
                <asp:TextBox ID="titreTextBox" runat="server" Text='<%# Bind("titre") %>'>
                </asp:TextBox><br />
                Texte :
                <asp:TextBox ID="infoTextBox" runat="server" Text='<%# Bind("info") %>' Height="153px" Width="563px" TextMode="MultiLine" MaxLength="250">
                </asp:TextBox><br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Mettre à jour">
                </asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Annuler">
                </asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                date:
                <asp:TextBox ID="dateTextBox" runat="server" Text='<%# Bind("date") %>'>
                </asp:TextBox><br />
                Titre:
                <asp:TextBox ID="titreTextBox" runat="server" Text='<%# Bind("titre") %>' Height="18px" Width="519px"></asp:TextBox><br />
                <br />
                Votre texte :<br />
                <asp:TextBox ID="infoTextBox" runat="server" Text='<%# Bind("info") %>' Height="153px" Width="563px" TextMode="MultiLine" MaxLength="250"></asp:TextBox><br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insérer">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Annuler">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
                Edito numéro :
                <asp:Label ID="idinfoLabel" runat="server" Text='<%# Eval("idinfo") %>'></asp:Label> Publié
                le:
                <asp:Label ID="dateLabel" runat="server" Text='<%# Bind("date") %>'></asp:Label><br /><br />
                Titre:
                <asp:Label ID="titreLabel" runat="server" Text='<%# Bind("titre") %>'></asp:Label><br /><br />
                Texte:
                <asp:Label ID="infoLabel" runat="server" Text='<%# Bind("info") %>'></asp:Label><br /><br />
                <div class="bouton_modif">
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Modifier"></asp:LinkButton>
                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Supprimer"></asp:LinkButton>
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                    Text="Nouveau"></asp:LinkButton></div>
            </ItemTemplate>
        </asp:FormView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:intranetConnectionString %>"
            DeleteCommand="DELETE FROM [infoaccueil] WHERE [idinfo] = @idinfo" InsertCommand="INSERT INTO [infoaccueil] ([date], [titre], [info]) VALUES (@date, @titre, @info)"
            SelectCommand="SELECT * FROM [infoaccueil] ORDER BY [idinfo] DESC" UpdateCommand="UPDATE [infoaccueil] SET [date] = @date, [titre] = @titre, [info] = @info WHERE [idinfo] = @idinfo">
            <DeleteParameters>
                <asp:Parameter Name="idinfo" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="date" Type="String" />
                <asp:Parameter Name="titre" Type="String" />
                <asp:Parameter Name="info" Type="String" />
                <asp:Parameter Name="idinfo" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="idinfo" Type="Int32" />
                <asp:Parameter Name="date" Type="String" />
                <asp:Parameter Name="titre" Type="String" />
                <asp:Parameter Name="info" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        </div>
    </asp:Content>

    Comme vous le voyez, j'insère une date dans ma base de données (en rouge). Je voudrais insérer la date du jour... j'ai essayé de la socker dans une String datenow puis j'ai mis @datenow à la place de date, mais j'ai une erreur (variable non déclaré). Le code pour récuperer la date, je 'lai mis dans

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
        {
     
        }

    comme ceci : String datenow = DateTime.Today.ToLongDateString();

    code récuperé dans un tororial.


    je l'ai mis dans :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
      protected void Page_Load(object sender, EventArgs e)
        {
     
            String dateToday = DateTime.Today.ToLongTimeString();
        }
    mais ça ne marche pas non plus...


    Merci d'avance pour votre aide....

  5. #5
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Points : 62
    Points
    62
    Par défaut
    Problème à demi réussi, j'ai fais comme suit :


    <InsertItemTemplate>
    date:
    <asp:TextBox ID="dateTextBox" runat="server" Text='<%# DateTime.Today.ToShortDateString() %>'></asp:TextBox><br />


    Mon champ est bien rempli par la date du jour mais lors de l'insertion, j'ai une erreur comme quoi il serait nulle....

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    581
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2004
    Messages : 581
    Points : 633
    Points
    633
    Par défaut
    hum ?? Tu as déjà fait du .net ???

    Click droit avec ta souris sur ta page html, la tu fais view code, et tu seras dans la partie serveur ou tu pourras plus facilement gérer les accès bd et autre.

  7. #7
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Points : 62
    Points
    62
    Par défaut
    Citation Envoyé par fix105
    hum ?? Tu as déjà fait du .net ???

    Click droit avec ta souris sur ta page html, la tu fais view code, et tu seras dans la partie serveur ou tu pourras plus facilement gérer les accès bd et autre.
    Non, je n'ai jamais fait de .net

Discussions similaires

  1. [AC-2013] remplacer le nom d'un formulaire par une variable dans un Public Sub
    Par GuyDuLac dans le forum VBA Access
    Réponses: 2
    Dernier message: 24/10/2014, 17h23
  2. [XL-2007] remplacer nom formulaire par une variable
    Par franc83 dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 28/12/2011, 18h07
  3. Réponses: 0
    Dernier message: 22/06/2010, 15h53
  4. Réponses: 1
    Dernier message: 19/04/2010, 13h04
  5. valeur d'un champ text modifié par une variable en parametre
    Par klimero dans le forum Général JavaScript
    Réponses: 21
    Dernier message: 21/04/2006, 10h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo