简体   繁体   中英

Access Control inside gridview template field

from last couple of days i am trying to access different control IDs in asp.net vb code file which i put inside gridview template field like TextBox , Label and Dropdown list. What i want to do is that i have an sql table "coursereg" with different columns. I put one gridview and added a template field in it and put "generate auto columns in gridview controls to false" I put an HTML table in that temp field and put some controls. Some controls in that table are bound with that sql table "coursereg" fields like text boxes and labels and some are open which user will fill himself. Now i have a submit button in that form which will add all the text to another sql Table called FA. Here i have got caught when i try to add the value/text of these controls with cmd.CommandText like cmd.Parameters.AddWithValue("@name", txtname.text.string) the textbox id txtname does not get populate and show the error the txtbox is not declared it may be inaccessible due to its protection level and like wise shows same error on all controls which i put inside gridview. Kindly help me in this regard plzzz i am learning asp.net (Vb ) my self and have not so much programming skills to tackle it out.

 Protected Sub Button1_Click(sender As Object, e As EventArgs)
    cmd.CommandText = "INSERT INTO FA (regno,photo,name,parentage,address,Phone,foccupation,income,category,class,rollno,course,subjects,actnumber,actname,acttype,bankname,branchname) " & _
               "values(@regno,@photo,@name,@parentage,@address,@Phone,@foccupation,@income,@category,@class,@rollno,@course,@subjects,@actnumber,@actname,@acttype,@bankname,@branchname)"
    cmd.Parameters.AddWithValue("@name", txtname.text.string)

    ' i (tried this but does not work)   Dim text As TextBox = TryCast(grd.Controls(0).Controls(0).FindControl("textname"), Text)
End Sub

<asp:TextBox ID="TextBox1" runat="server" Enabled="False" ReadOnly="True" Text='<%# String.Format("{0} {1} {2}", Eval("sfname"), Eval("smname"), Eval("slname")) %>' Height="22px" Width="308px" Font-Size="12pt"></asp:TextBox>

Think about it. You have a grid and you added some textbox (txtname) to the row template. That means that you won't have a single textbox created, but as many as there are rows in your grid. So that is why you can't just simply access that txtname from the Button1_Click.

It is not clear whether you want to call that INSERT statement multiple times for all the rows, but if yes, then you can use something like this (sorry for the C# code, but you can probably find out how to convert that to VB.NET):

foreach (GridViewRow row in grd.Rows)
{
    var txt = row.FindControl("textname") as TextBox;
    if (txt != null)
    {
       // INSERT STATEMENT with txt.Text
    }
}

IMPORTANT: The main point of attention in this task is how you actually bind the grid. You have to take page life cycle into the consideration. If you bind your grid on Page_Load then the you might get empty values of textboxes instead of what you actually typed. That is because Button1_Click event handler goes after the Page_Load and LoadPostData goes before the PageLoad. LoadPostData is the event in page life cycle when values of serverside inputs (like all the txtname textboxes) get copied from what you typed on the clientside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM