0

I have some code and it is unable to take the values entered from a dropdown list or textbox and output them to a SharePoint List. here is the code

        try
        {
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            SPList List = web.Lists["FORM D List"];
            SPListItem NewItem = List.Items.Add();
            string title = "this is new";
            NewItem["Title"] = title;
            NewItem.Update();

          //  NewItem.Update();


            foreach (GridViewRow row in FormDGrid.Rows)
           // for (int count = 0; count < FormDGrid.Rows.Count; count++)
            {
                NewItem["State"] = ((Label)FormDGrid.FindControl("StateField")).Text;
                NewItem["County"] = ((DropDownList)FormDGrid.FindControl("CountyField")).Text;
                NewItem["City or Town"] = ((TextBox)FormDGrid.FindControl("CityField")).Text;
                NewItem.Update();
                List.Update();

            }

thoughts? the assignment of Title column works directly but if I try to link to a control within the GridView it fails.

edit: here is the code on the aspcx

<asp:TemplateField HeaderText="State" HeaderStyle-Width="70">
<ItemTemplate>
<asp:Label CssClass="ms-input" Width="70"
   ID="lblState" Text='<%# DataBinder.Eval(Container.DataItem, "state")%>'   runat='server' />                       
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="County">
<ItemTemplate>
<asp:DropDownList ID="lblCounty" runat="server" CssClass="ms-input"  Width="150">
<asp:ListItem Value="01">--select County here--</asp:ListItem>
<asp:ListItem Value="02">Columbia</asp:ListItem>
<asp:ListItem Value="03">Charleston</asp:ListItem>
        </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>



<asp:TemplateField HeaderText="City or Town" HeaderStyle-Width="270" SortExpression="lll">
<ItemTemplate>
<asp:TextBox CssClass="ms-input" TextMode="SingleLine" MaxLength="3700" Width="270"
   ID="lblCity" Text='<%# DataBinder.Eval(Container.DataItem, "cityorTown") %>' runat='server' />                       
</ItemTemplate>
</asp:TemplateField>
3
  • Could you post the code on the ascx / aspx page? And if you put the code inside a try catch and print out the exception on the web part / application page, what is the specific error message? – AWJ Feb 22 '18 at 8:47
  • alright, just posted it up. not sure on your 2nd question, no error gets returned. – prejt Feb 22 '18 at 16:00
  • It should be row.FindControl instead of FormDGrid.FindControl, as you are looking for control in row of a grid – P S Feb 23 '18 at 11:21
0

You should put the Control's ID inside the FindControl method, such as:

foreach (GridViewRow row in FormDGrid.Rows)
       // for (int count = 0; count < FormDGrid.Rows.Count; count++)
        {
            NewItem["State"] = ((Label)FormDGrid.FindControl("lblState")).Text;
            NewItem["County"] = ((DropDownList)FormDGrid.FindControl("lblCounty")).Text;
            NewItem["City or Town"] = ((TextBox)FormDGrid.FindControl("lblCity")).Text;
            NewItem.Update();
            List.Update();

        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.